Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TryParse special date-format

Tags:

c#

datetime

I try to parse a date-time stamp from an external system as following:

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

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

sl3dg3

like image 629
sl3dg3 Avatar asked Apr 28 '11 10:04

sl3dg3


2 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);
like image 67
Jon Skeet Avatar answered Oct 05 '22 10:10

Jon Skeet


you can use DateTime.TryParseExact instead.

How to create a .NET DateTime from ISO 8601 format

like image 44
Mubashir Khan Avatar answered Oct 05 '22 09:10

Mubashir Khan