Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting String Format "yyyy-MM-ddTHH:mm:ss.fffZ" to DateTime

Tags:

c#

datetime

I know this question has been asked a number of different ways, and I have looked at them all and none of the solutions seem to work for me. So, I am hoping that maybe you guys can give me a quick hand.

The input string is: "2000-01-01T12:00:000Z". I need to take that input string and convert it to DateTime so that it can be stored in the database.

I have been using ParseExact, but I keep getting the not recognized date string exception. Where am I going wrong?

inValue.LatestDepartTime = "2000-01-01T12:00:000Z";
DateTime _latestDepartTime = DateTime.ParseExact(inValue.LatestDepartTime, "yyyy-MM-dd HH:mm:ss.fff", CultureInfo.InvariantCulture);
like image 595
CalvinBlount Avatar asked Feb 05 '12 19:02

CalvinBlount


2 Answers

Your format string needs to exactly match the input.

That includes the literal T and Z characters.

like image 60
SLaks Avatar answered Oct 03 '22 18:10

SLaks


Use yyyy-MM-dd'T'HH:mm:ss.fff'Z'

The code is:

public DateTime convertIsoToDateTime (string iso)
{
    return DateTime.ParseExact(iso, "yyyy-MM-dd'T'HH:mm:ss.fff'Z'", CultureInfo.InvariantCulture);
}
like image 44
Mohammad f Avatar answered Oct 03 '22 19:10

Mohammad f