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

前端 未结 8 652

I want to format the input string into MM/dd/yyyy hh:mm:ss format in C#.
The input string is in format MM/dd/yyyy hh:mm:ss
For example :

8条回答
  •  暖寄归人
    2020-12-03 09:12

    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.

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

提交回复
热议问题