1

I have the following string:

20150521T205510Z

How do I go about converting this to DateTime? Do I need to parse the date first then time? Or is there a way to plug this into DateTime.Parse() to get the correct value?

Sean P
  • 949
  • 4
  • 22
  • 41

2 Answers2

3

You could try something like this:

var dateString = "20150521T205510Z";

var date = DateTime.ParseExact(dateString,
                   "yyyyMMdd'T'HHmmss'Z'",
                   CultureInfo.InvariantCulture);

I referenced the answer from: DateTime.Parse("2012-09-30T23:00:00.0000000Z") always converts to DateTimeKind.Local

Community
  • 1
  • 1
austin wernli
  • 1,801
  • 12
  • 15
  • Thanks this made sense. I didnt know that there was a built in way to handle dates with other characters thrown in. – Sean P May 26 '15 at 18:27
0

Use function DateTime.ParseExact.

Additional information: https://msdn.microsoft.com/en-us/library/w2sa9yss%28v=vs.110%29.aspx

Riad Baghbanli
  • 3,105
  • 1
  • 12
  • 20