How to convert a string containing AM/PM to DateTime?

后端 未结 4 2020
离开以前
离开以前 2020-12-20 11:59

How can i parse a string like this: \"2/22/2015 9:54:02 AM\" to a DateTime instance?

i am currently using the DateTime.ParseExact method but without the AM/PM i.e:

4条回答
  •  感情败类
    2020-12-20 12:43

    try this:

    if (date.Contains("AM") || date.Contains("PM"))
        return DateTime.ParseExact(date, "dd.MM.yyyy h:mm:ss tt", CultureInfo.InvariantCulture);
    return DateTime.ParseExact(date, "dd.MM.yyyy HH:mm:ss", CultureInfo.InvariantCulture);
    

提交回复
热议问题