DateTime问题(字符串不是有效的日期时间) [英] DateTime Issue(string is not a valid datetime)

查看:253
本文介绍了DateTime问题(字符串不是有效的日期时间)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




当我将日期转换为格式时说dd / MM / yyy

Hi
When I convert the date to format say "dd/MM/yyy"

String strdate = Convert.ToDateTime(DateTime.Now.ToString()).ToString("dd-MM-yyyy")



我收到此错误字符串不是有效的日期时间但是当我写下面的时候我我没有收到任何错误。为什么会这样?


I am receiving this error "String is not valid date time" but when I write like below I am not receiving any error. Why is it happening like this?

String strDate = DateTime.ParseExact(Convert.ToDateTime(DateTime.Now.ToString()).ToString("dd-MM-yyyy"),"dd-MM-yyyy",CultureInfo.InvariantCulture).ToString("dd-MM-yyyy")

推荐答案

你coudlt没有转换.MM / dd / yyyy格式只转换日期时间数据类型其他明智的你没有转换,日期时间数据类型转换只有MM / dd / yyyy格式只有chage,只有你将转换这个错误做所有的开发人员第一次



You coudlt not convert .MM/dd/yyyy format only convert Datetime data type other wise you coudlt not convert,Date Time data type convert only MM/dd/yyyy format only chage that after only you will convert This mistake do all the developer first time

public const string DEFAULT_DATE_FORMAT = "dd/MM/yyyy";
public static string ConvertDtFormat(string dateTimeString)
           {
               dateTimeString = dateTimeString.Trim();
               while (dateTimeString.Contains("  "))
               {
                   dateTimeString = dateTimeString.Replace("  ", " ");
               }

               if (dateTimeString == null || dateTimeString.Length == 0)
               {
                   return string.Empty;
               }
               else
               {
                   DateTime convertedDateTime = new DateTime();
                   string userDateFormat =DEFAULT_DATE_FORMAT; 

                   try
                   {

                       if (userDateFormat.Trim().Contains("dd/MM/yyyy"))
                           convertedDateTime = DateTime.ParseExact(dateTimeString, format_dmy, CultureInfo.InvariantCulture,
                                                                   DateTimeStyles.AllowLeadingWhite | DateTimeStyles.AllowTrailingWhite);
                       else if (userDateFormat.Trim().Contains("MM/dd/yyyy"))
                           convertedDateTime = DateTime.ParseExact(dateTimeString, format_mdy, CultureInfo.InvariantCulture,
                                                                   DateTimeStyles.AllowLeadingWhite | DateTimeStyles.AllowTrailingWhite);

                       return convertedDateTime.ToString("MMM dd, yyyy hh:mm tt");
                   }
                   catch
                   {
                       return "Invalid DateTime";
                   }
               }
           }


这是无意义的采取DateTime,将其转换为字符串,并将其转换回来到DateTime。

This is nonsense to take a DateTime, convert it to string, and convert it back to DateTime.
string strdate = DateTime.Now.ToString("dd-MM-yyyy");



反之:


And the inverse:

DateTime myDate = DateTime.ParseExact("29-09-2013", "dd-MM-yyyy", CultureInfo.InvariantCulture);



始终应用KISS规则(保持简单和愚蠢)。


Always apply the KISS rule (Keep It Simple and Stupid).


2行编码

datetime strdate = Convert.ToDateTime(DateTime.Now.ToString())。ToString(dd-MM-yyyy)

string s = strdate.tostring(dd / MM / yyyy)
2 line coding
datetime strdate =Convert.ToDateTime(DateTime.Now.ToString()).ToString("dd-MM-yyyy")
string s=strdate.tostring(dd/MM/yyyy)


这篇关于DateTime问题(字符串不是有效的日期时间)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆