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

前端 未结 8 679

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:26

    You can use DateTime.ParseExact() method.

    Converts the specified string representation of a date and time to its DateTime equivalent using the specified format and culture-specific format information. The format of the string representation must match the specified format exactly.

    DateTime date = DateTime.ParseExact("04/30/2013 23:00", 
                                        "MM/dd/yyyy HH:mm", 
                                        CultureInfo.InvariantCulture);
    

    Here is a DEMO.

    hh is for 12-hour clock from 01 to 12, HH is for 24-hour clock from 00 to 23.

    For more information, check Custom Date and Time Format Strings

提交回复
热议问题