简体   繁体   English

C#错误“字符串未被识别为有效的DateTime”

[英]C# error “String was not recognized as a valid DateTime”

I am trying to save the DateTime.Now in the format "yyyyMMdd" 我试图以“yyyyMMdd”格式保存DateTime.Now

I have this code 我有这个代码

string todaysDate = DateTime.Now.ToString();

... ...

U_Date_of_PD_added = GetDateFromString(todaysDate) 

// U_Date_of_PD_added is a datetime Database field // U_Date_of_PD_added是日期时间数据库字段

... ...

//Method to get date from string
private DateTime GetDateFromString(string dateString)
{
   string format = "yyyyMMdd";
   return DateTime.ParseExact(dateString, format, CultureInfo.InvariantCulture);
}

I keep getting the error "String was not recognized as a valid DateTime." 我一直收到错误“字符串未被识别为有效的DateTime”。 as it tries to parse. 因为它试图解析。 What could be wrong? 可能有什么不对?

I do not care if it saves the time, I would prefer 00:00:00.000 我不在乎是否节省时间,我宁愿00:00:00.000

将日期转换为字符串时,还需要提供格式字符串:

string todaysDate = DateTime.Now.ToString("yyyyMMdd", CultureInfo.InvariantCulture);

First you get today's date and then convert in string as follow and you are done. 首先你得到今天的日期然后转换为字符串如下,你就完成了。

DateTime today = DateTime.Today.Date;
string date = today.ToString("yyyy/MM/dd");
DateTime.Now.ToString("yyyyMMdd", CultureInfo.InvariantCulture);

will give you the current date. 会给你当前的日期。 Note that it's important the CultureInfo.InvariantCulture . 请注意, CultureInfo.InvariantCulture很重要。 If you don't trust it, try: 如果您不信任它,请尝试:

var r = DateTime.Now.ToString("yyyyMMdd", new CultureInfo("ar-SA")); // 14321128
var r2 = DateTime.Now.ToString("yyyyMMdd", CultureInfo.InvariantCulture); // 20111026

DateTime.Now.ToString() results in a format that is region specific, but it mostly contains the time. DateTime.Now.ToString()一种特定于区域的格式,但它主要包含时间。 This format is called "General date/time pattern (long time)." 此格式称为“常规日期/时间模式(长时间)”。

So trying to parse it with your format string will fail because your format string does not contain information about the time component. 因此,尝试使用您的格式字符串解析它将失败,因为您的格式字符串不包含有关时间组件的信息。

You can use DateTime.TryParse without providing a format string or you have to provide your format string to your call to DateTime.Now.ToString as well. 您可以在不提供格式字符串的情况下使用DateTime.TryParse,也可以在调用DateTime.Now.ToString时提供格式字符串。

The value of todaysDate cannot be converted to a DateTime object by using DateTime.Now.ToString() because DateTime.Now.ToString() results in a value that uses the default date format ( 26-10-2011 14:02:31 ). 使用DateTime.Now.ToString()无法将todaysDate的值转换为DateTime对象,因为DateTime.Now.ToString()会生成使用默认日期格式的值( 26-10-2011 14:02:31 ) 。 Try doing this instead: string todaysDate = DateTime.Now.ToString("yyyyMMdd"); 请尝试这样做: string todaysDate = DateTime.Now.ToString("yyyyMMdd"); and you should be able to convert it back using your GetDateFromString method. 你应该能够使用GetDateFromString方法将其转换回来。

DateTime.Now.ToString("yyyyMMdd")将返回20110926的字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM