DateTime.ParseExact FormatException String was not recognized as a valid DateTime

后端 未结 2 1758
不知归路
不知归路 2021-01-12 01:39

I\'m completely stumped on this one. As far as I can see the documentation and other posts on SO I\'ve read say this should work. I must be missing something silly, but I ju

相关标签:
2条回答
  • 2021-01-12 02:01

    Does this work

     string myDate = "30-12-1899 07:50:00:AM";
    DateTime dt1 = DateTime.ParseExact(myDate, "dd-MM-yyyy hh:mm:ss:tt", 
                                               CultureInfo.InvariantCulture)
    
    0 讨论(0)
  • 2021-01-12 02:10
    string myDate = "30-12-1899 07:50:00:AM";
    DateTime dt1 = DateTime.ParseExact(myDate, "dd-MM-yyyy HH:mm:ss:tt",  
                                               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.

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

    0 讨论(0)
提交回复
热议问题