Custom TryParse Date Format

I am trying to parse a date and time stamp from an external system as follows:

DateTime expiration; DateTime.TryParse("2011-04-28T14:00:00", out expiration); 

Unfortunately, it is not recognized. How can I analyze this successfully?

sl3dg3

+7
source share
5 answers

Specify the exact format you want in DateTime.TryParseExact :

 DateTime expiration; string text = "2011-04-28T14:00:00"; bool success = DateTime.TryParseExact(text, "yyyy-MM-ddTHH:mm:ss", CultureInfo.InvariantCulture, DateTimeStyles.None, out expiration); 
+11
source

you can use DateTime.TryParseExact instead.

How to create .NET DateTime from ISO 8601 format

+2
source

try it

 DateTime expiration; DateTime.TryParse("2011-04-28 14:00:00", out expiration); 

Without the use of "T".

+2
source

You need to add "00000Z" to your string argument.

 DateTime.TryParse("2011-04-28T14:00:0000000Z", out expiration); 
+1
source

The user DateTime.TryParseExact function is as follows:

 DateTime dateValue; if (DateTime.TryParseExact(dateString, "yyyy-MM-ddTHH:mm:ss", new CultureInfo("en-US"), DateTimeStyles.None, out dateValue)) { // Do Something .. } 

Read about DateTime .

+1
source

All Articles