简体   繁体   English

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

[英]Datetime format Issue: String was not recognized as a valid DateTime

I want to format the input string into MM/dd/yyyy hh:mm:ss format in C#. 我想在C#中将输入字符串格式化为MM / dd / yyyy hh:mm:ss格式。
The input string is in format MM/dd/yyyy hh:mm:ss 输入字符串的格式为MM/dd/yyyy hh:mm:ss
For example : "04/30/2013 23:00" 例如: "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. 我尝试了Convert.ToDateTime()函数,但它认为4是日期,3是月份,这不是我想要的。 Actually month is 04 and date is 03. 实际上月是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。

Your date time string doesn't contains any seconds. 您的日期时间字符串不包含任何秒数。 You need to reflect that in your format (remove the :ss ). 您需要以您的格式反映出来(删除:ss )。
Also, you need to specify H instead of h if you are using 24 hour times: 此外,如果您使用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 自定义日期和时间格式字符串

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. 使用指定的格式和特定​​于区域性的格式信息将指定的日期和时间字符串表示形式转换为其DateTime等效形式。 The format of the string representation must match the specified format exactly. 字符串表示的格式必须与指定的格式完全匹配。

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 有关更多信息,请查看Custom Date and Time Format Strings

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);
 }

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)

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)。

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);

This can also be the problem if your string is 6/15/2019. 如果您的字符串是6/15/2019,这也可能是问题。 DateTime Parse expects it to be 06/15/2019. DateTime Parse预计它将是06/15/2019。

So first split it by slash 所以首先用斜线拆分它

var dateParts = "6/15/2019"
var month = dateParts[0].PadLeft(2, '0');
var day = dateParts[1].PadLeft(2, '0');
var year = dateParts[2] 


var properFormat = month + "/" +day +"/" + year;

Now you can use DateTime.Parse(properFormat, "MM/dd/yyyy"). 现在你可以使用DateTime.Parse(properFormat,“MM / dd / yyyy”)。 It is very strange but this is only thing working for me. 这很奇怪,但这只对我有用。

You may use this type format (get formatted data from sql server) 您可以使用此类型格式(从sql server获取格式化数据)


FORMAT(convert(datetime,'16/04/2018 10:52:20',103),'dd/MM/yyyy HH:mm:ss', 'en-us') 格式(转换(日期时间,'16 / 04/2018 10:52:20',103),'dd / MM / yyyy HH:mm:ss','en-us')


CONVERT(VARCHAR,convert(datetime,'16/04/2018 10:52:20',103), 120) CONVERT(VARCHAR,convert(datetime,'16 / 04/2018 10:52:20',103),120)

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

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