简体   繁体   中英

Convert this custom string format into DateTime?

How to convert the following string

09/11/2011 9:40:55 pm
    20/11/2011 3:40:55 am

into C# DateTime ?

Use DateTime.ParseExact .

string dateString = "20/11/2011 3:40:55 am";
DateTime parsedDate = DateTime.ParseExact(
   dateString,
   "dd/MM/yyyy h:mm:ss tt",
   CultureInfo.InvariantCulture);

DateTime.Parse:

http://msdn.microsoft.com/en-us/library/1k1skd40.aspx

See also:

http://msdn.microsoft.com/en-us/library/system.globalization.datetimeformatinfo.timeseparator.aspx

http://msdn.microsoft.com/en-us/library/system.datetime.aspx

For example:

string dateString = "5/1/2008 8:30:52 AM";
DateTime date1 = DateTime.Parse(dateString, 
                          System.Globalization.CultureInfo.InvariantCulture); 
DateTime DT_1 = new DateTime(2011, 11, 9, 21, 40, 55);
DateTime DT_2 = new DateTime(2011, 11, 20, 15, 40, 55);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM