I’m working on my first full C# project. I’m writing a portable class library for a Windows Store App / Windows Phone app and I’m actually having trouble with just DateTime.ParseExact and DateTime.TryParseExact.
The string I’m attempting to convert to a DateTime is in fact in Zulu. The example looks like these:
2012-09-14T04:42:25.117Z
2012-08-17T04:39:51.215Z
As you can see, we get down to milliseconds here. The following is the code being used:
DateTime _createdAt = DateTime.ParseExact(dateTime, TIMEFORMATTER,
CultureInfo.InvariantCulture,
DateTimeStyles.AssumeUniversal);
return _createdAt;
And TIMEFORMATTER is a const declared up top:
const string TIMEFORMATTER = "yyyy-MM-ddTHH:mm:ss.fffZ"
I have been reading a number of SO posts looking for something that solves it. I figured that this one would just fine but it did not.
If anyone could point me in the right direction I would appreciate it. I have only worked with the Date object in VB.net.
Edit: Adding additional code from the library to show the full explanation
if (details.ContainsKey("created_at"))
{
var time = details["created_at"].Stringify();
Debug.WriteLine(time);
DateTime _createdAt = FormatDateTime(time);
Debug.WriteLine(_createdAt.ToLocalTime());
Asana.Projects[indexOfProject].CreatedAt = _createdAt;
}
public static DateTime FormatDateTime(string dateTime)
{
DateTime _createdAt = new DateTime();
// 2012-09-14T04:42:25.117Z
DateTime.TryParse(dateTime, out _createdAt);
return _createdAt;
}
This is what goes to debug:
"2012-08-17T04:39:52.878Z"
1/1/0001 12:00:00 AM
"2012-08-17T05:17:12.530Z"
1/1/0001 12:00:00 AM
"2012-09-09T04:23:08.805Z"
1/1/0001 12:00:00 AM
"2012-08-17T04:39:51.215Z"
1/1/0001 12:00:00 AM
"2012-09-14T05:12:22.056Z"
1/1/0001 12:00:00 AM
"2012-08-06T04:51:10.662Z"
1/1/0001 12:00:00 AM
"2012-09-14T04:40:56.602Z"
1/1/0001 12:00:00 AM
"2012-08-17T04:52:33.264Z"
1/1/0001 12:00:00 AM
"2012-09-14T04:43:50.953Z"
1/1/0001 12:00:00 AM
"2012-09-13T06:07:16.530Z"
1/1/0001 12:00:00 AM
"2012-09-14T04:42:25.117Z"
1/1/0001 12:00:00 AM
Solution
With the help of Jeremy Thompson, I was actually able to debug my own mistake. Turns out this is my first time working with JSON data in .Net as well (used to Python).
.Stringify() and .GetString() do entirely different things when it comes to getting the text of the string. I opened a new console program and used Jeremy’s code to produce the same results he did. Worked flawlessly. When I went back to my library, I was getting format problems on some of the strings I was getting back. Then it hit me that in the debug window I was getting quotes around the JSON data.
The new working code is as follows:
// From the depths of a method
if (details.ContainsKey("created_at"))
{
var time = details["created_at"].GetString();
DateTime _createdAt = FormatDateTime(time);
Asana.Projects[indexOfProject].CreatedAt = _createdAt;
Debug.WriteLine(Asana.Projects[indexOfProject].CreatedAt.ToLocalTime());
}
And the new FormatDateTime method (not that new as it turns out…):
public static DateTime FormatDateTime(string dateTime)
{
DateTime _createdAt = new DateTime();
DateTime.TryParseExact(dateTime, TIMEFORMATTER, CultureInfo.InvariantCulture,
DateTimeStyles.AssumeUniversal, out _createdAt);
return _createdAt;
}
Some example input/output:
2012-09-14T04:43:42.060Z
9/13/2012 10:43:42 PM
2012-09-09T04:23:08.805Z
9/8/2012 10:23:08 PM
2012-08-06T04:51:10.662Z
8/5/2012 10:51:10 PM
Thank you so much to those that were looking and thinking on it.
eg:
Edit:
In the MSDN DateTime.TryParseExact Method:
I see you are suppplying the Date component. My only guess is that I’m in Australia and it works and we use DD-MM-YYYY and in the US its MM-DD-YYYY. Perhaps flip it around and see if that works, eg:
“2012-17-08T04:39:52.878Z”