Why can’t DateTime.ParseExact() parse the AM/PM in “4/4/2010 4:20:00 PM” using “M'/'d'/'yyyy H':'mm':'ss' 'tt”

前端 未结 3 2010
独厮守ぢ
独厮守ぢ 2020-11-28 12:26

I\'m using c#, and if I do

DateTime.ParseExact(\"4/4/2010 4:20:00 PM\", \"M\'/\'d\'/\'yyyy H\':\'mm\':\'ss\' \'tt\", null)

The return value

3条回答
  •  南方客
    南方客 (楼主)
    2020-11-28 12:55

    You need to use a lowercase "h" for the hour argument in the format string. The uppercase "H" represents 24-hour time, so "4" is recognized as 4 AM (since "16" would be 4 PM).

    DateTime.ParseExact("4/4/2010 4:20:00 PM", "M/d/yyyy h:mm:ss:tt", null)
    

提交回复
热议问题