Converting a String to DateTime

后端 未结 17 2999
南方客
南方客 2020-11-21 06:12

How do you convert a string such as 2009-05-08 14:40:52,531 into a DateTime?

17条回答
  •  天命终不由人
    2020-11-21 06:29

    You could also use DateTime.TryParseExact() as below if you are unsure of the input value.

    DateTime outputDateTimeValue;
    if (DateTime.TryParseExact("2009-05-08 14:40:52,531", "yyyy-MM-dd HH:mm:ss,fff", System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, out outputDateTimeValue))
    {
        return outputDateTimeValue;
    }
    else
    {
        // Handle the fact that parse did not succeed
    }
    

提交回复
热议问题