日期时间格式问题:字符串未被识别为有效的DateTime

时间:2022-05-13 10:41:29

I want to format the input string into MM/dd/yyyy hh:mm:ss format in C#.
The input string is in format MM/dd/yyyy hh:mm:ss
For example :"04/30/2013 23:00"

我想在C#中将输入字符串格式化为MM / dd / yyyy hh:mm:ss格式。输入字符串格式为MM / dd / yyyy hh:mm:ss例如:“04/30/2013 23:00”

I tried Convert.ToDateTime() function, but it considers 4 as date and 3 as month which is not what I want. Actually month is 04 and date is 03.

我尝试了Convert.ToDateTime()函数,但它认为4为日期,3为月份,这不是我想要的。实际上月是04,日期是03。

I tried DateTime.ParseExact() function also, But getting Exception.

我也尝试了DateTime.ParseExact()函数,但是得到了Exception。

I am getting error:

我收到错误:

String was not recognized as a valid DateTime.

字符串未被识别为有效的DateTime。

6 个解决方案

#1


12  

Your date time string doesn't contains any seconds. You need to reflect that in your format (remove the :ss).
Also, you need to specify H instead of h if you are using 24 hour times:

您的日期时间字符串不包含任何秒数。您需要以您的格式反映出来(删除:ss)。此外,如果您使用24小时,则需要指定H而不是h:

DateTime.ParseExact("04/30/2013 23:00", "MM/dd/yyyy HH:mm", CultureInfo.InvariantCulture)

See here for more information:

浏览此处获取更多信息:

Custom Date and Time Format Strings

自定义日期和时间格式字符串

#2


5  

You can use DateTime.ParseExact() method.

您可以使用DateTime.ParseExact()方法。

Converts the specified string representation of a date and time to its DateTime equivalent using the specified format and culture-specific format information. The format of the string representation must match the specified format exactly.

使用指定的格式和特定​​于区域性的格式信息将指定的日期和时间字符串表示形式转换为其DateTime等效形式。字符串表示的格式必须与指定的格式完全匹配。

DateTime date = DateTime.ParseExact("04/30/2013 23:00", 
                                    "MM/dd/yyyy HH:mm", 
                                    CultureInfo.InvariantCulture);

Here is a DEMO.

这是一个DEMO。

hh is for 12-hour clock from 01 to 12, HH is for 24-hour clock from 00 to 23.

hh是从01到12的12小时制,HH是从00到23的24小时制。

For more information, check Custom Date and Time Format Strings

有关更多信息,请查看自定义日期和时间格式字符串

#3


3  

try this:

尝试这个:

string strTime = "04/30/2013 23:00";
DateTime dtTime;
if(DateTime.TryParseExact(strTime, "MM/dd/yyyy HH:mm",  
   System.Globalization.CultureInfo.InvariantCulture, 
   System.Globalization.DateTimeStyles.None, out dtTime))
 {
    Console.WriteLine(dtTime);
 }

#4


1  

change the culture and try out like this might work for you

改变文化,尝试这样可能对你有用

string[] formats= { "MM/dd/yyyy HH:mm" }
var dateTime = DateTime.ParseExact("04/30/2013 23:00", 
     formats, new CultureInfo("en-US"), DateTimeStyles.None);

Check for details : DateTime.ParseExact Method (String, String[], IFormatProvider, DateTimeStyles)

检查详细信息:DateTime.ParseExact方法(String,String [],IFormatProvider,DateTimeStyles)

#5


0  

DateTime dt1 = DateTime.ParseExact([YourDate], "dd-MM-yyyy HH:mm:ss",  
                                           CultureInfo.InvariantCulture);

Note the use of HH (24-hour clock) rather than hh (12-hour clock), and the use of InvariantCulture because some cultures use separators other than slash.

注意使用HH(24小时制)而不是hh(12小时制),以及使用InvariantCulture,因为有些文化使用除斜线以外的分隔符。

For example, if the culture is de-DE, the format "dd/MM/yyyy" would expect period as a separator (31.01.2011).

例如,如果文化是de-DE,则格式“dd / MM / yyyy”将期望作为分隔符(31.01.2011)。

#6


0  

Below code worked for me:

下面的代码对我有用:

string _stDate = Convert.ToDateTime(DateTime.Today.AddMonths(-12)).ToString("MM/dd/yyyy");
String format ="MM/dd/yyyy";
IFormatProvider culture = new System.Globalization.CultureInfo("fr-FR", true);
DateTime _Startdate = DateTime.ParseExact(_stDate, format, culture);

#1


12  

Your date time string doesn't contains any seconds. You need to reflect that in your format (remove the :ss).
Also, you need to specify H instead of h if you are using 24 hour times:

您的日期时间字符串不包含任何秒数。您需要以您的格式反映出来(删除:ss)。此外,如果您使用24小时,则需要指定H而不是h:

DateTime.ParseExact("04/30/2013 23:00", "MM/dd/yyyy HH:mm", CultureInfo.InvariantCulture)

See here for more information:

浏览此处获取更多信息:

Custom Date and Time Format Strings

自定义日期和时间格式字符串

#2


5  

You can use DateTime.ParseExact() method.

您可以使用DateTime.ParseExact()方法。

Converts the specified string representation of a date and time to its DateTime equivalent using the specified format and culture-specific format information. The format of the string representation must match the specified format exactly.

使用指定的格式和特定​​于区域性的格式信息将指定的日期和时间字符串表示形式转换为其DateTime等效形式。字符串表示的格式必须与指定的格式完全匹配。

DateTime date = DateTime.ParseExact("04/30/2013 23:00", 
                                    "MM/dd/yyyy HH:mm", 
                                    CultureInfo.InvariantCulture);

Here is a DEMO.

这是一个DEMO。

hh is for 12-hour clock from 01 to 12, HH is for 24-hour clock from 00 to 23.

hh是从01到12的12小时制,HH是从00到23的24小时制。

For more information, check Custom Date and Time Format Strings

有关更多信息,请查看自定义日期和时间格式字符串

#3


3  

try this:

尝试这个:

string strTime = "04/30/2013 23:00";
DateTime dtTime;
if(DateTime.TryParseExact(strTime, "MM/dd/yyyy HH:mm",  
   System.Globalization.CultureInfo.InvariantCulture, 
   System.Globalization.DateTimeStyles.None, out dtTime))
 {
    Console.WriteLine(dtTime);
 }

#4


1  

change the culture and try out like this might work for you

改变文化,尝试这样可能对你有用

string[] formats= { "MM/dd/yyyy HH:mm" }
var dateTime = DateTime.ParseExact("04/30/2013 23:00", 
     formats, new CultureInfo("en-US"), DateTimeStyles.None);

Check for details : DateTime.ParseExact Method (String, String[], IFormatProvider, DateTimeStyles)

检查详细信息:DateTime.ParseExact方法(String,String [],IFormatProvider,DateTimeStyles)

#5


0  

DateTime dt1 = DateTime.ParseExact([YourDate], "dd-MM-yyyy HH:mm:ss",  
                                           CultureInfo.InvariantCulture);

Note the use of HH (24-hour clock) rather than hh (12-hour clock), and the use of InvariantCulture because some cultures use separators other than slash.

注意使用HH(24小时制)而不是hh(12小时制),以及使用InvariantCulture,因为有些文化使用除斜线以外的分隔符。

For example, if the culture is de-DE, the format "dd/MM/yyyy" would expect period as a separator (31.01.2011).

例如,如果文化是de-DE,则格式“dd / MM / yyyy”将期望作为分隔符(31.01.2011)。

#6


0  

Below code worked for me:

下面的代码对我有用:

string _stDate = Convert.ToDateTime(DateTime.Today.AddMonths(-12)).ToString("MM/dd/yyyy");
String format ="MM/dd/yyyy";
IFormatProvider culture = new System.Globalization.CultureInfo("fr-FR", true);
DateTime _Startdate = DateTime.ParseExact(_stDate, format, culture);