0

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.

Community
  • 1
  • 1
cdownard
  • 219
  • 2
  • 14
  • 1
    Are you getting an error or something? Also, the Date object in C# is the same as the one in VB.net. – Shane Andrade Feb 06 '13 at 05:01
  • 1
    Based on the SO post you linked to, and your own TIMEFORMATTER, have you tried to enclose the T and the Z in '', as in const string TIMEFORMATTER = "yyyy-MM-dd'T'HH:mm:ss.fff'Z'"? – Riaan Feb 06 '13 at 05:07
  • I forgot to add the exception: `A first chance exception of type 'System.FormatException' occurred in mscorlib.dll` Which is a problem with the formatter. I also tried to do a version that had `"yyyy-MM-dd'T'HH:mm:ss.fff'Z'"` and a lowercase z as other SO questions had suggested. – cdownard Feb 06 '13 at 05:13
  • Using my method of `Date.TryParse` will overcome the exception and still give you the milliseconds. Just saw the link and you cant get any better advice than Jon's. Why when you follow his approach doesn't it work??? – Jeremy Thompson Feb 06 '13 at 05:16
  • I added additional code snippets to the post to give more information. – cdownard Feb 06 '13 at 05:27
  • -1 because I've read through the question twicethree times and I am still confused. **Make sure to post a clear description of the problem (and a question).** Remove irrelevant details as *more is not always better*; links to other answers are fine, but don't include code unless it's actually used. If there is an exception, say what it is, where it is, and what input causes it .. if it "doesn't work" in other ways, explain them. Minimal test-cases are best. –  Feb 06 '13 at 05:31
  • The edited code snippet had the issue in it. The code listed above were all the relevant lines and directly related to the problem, generated/demonstrated issue, and eventual resolution. It was indeed a mistake to not paste in the Format Exception. – cdownard Feb 06 '13 at 06:16

1 Answers1

0
string stringdate = "2012-09-14T04:42:25.117Z";
DateTime date = new DateTime();
DateTime.TryParse(stringdate,out date);
MessageBox.Show(date.ToShortDateString());

eg:

MessageBox.Show(date.ToLocalTime().ToString());

enter image description here

Edit:

In the MSDN DateTime.TryParseExact Method:

The string parameter is parsed using default values. No white space other than that present in format is allowed. If string lacks a date component, the date of the returned DateTime value is set to 1/1/0001.

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"

Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321
  • 1
    `"2012-08-17T04:39:52.878Z"` `1/1/0001 12:00:00 AM` The first is the json string I get back from the API I scrape. The second is what gets returned after doing DateTime.TryParse. I'm not sure why it's not working for me. I'm happy to post more code. – cdownard Feb 06 '13 at 05:19
  • That gives me: 17/08/2012 2:39:52 PM, so you're converting Zulu date times and your in the US and I'm downunder. Works for me doesn't work for you. Thats strange, let me have a think about what else it could be. – Jeremy Thompson Feb 06 '13 at 05:20
  • `var time = details["created_at"].Stringify(); Debug.WriteLine(time); DateTime _createdAt = FormatDateTime(time); Debug.WriteLine(_createdAt.ToLocalTime());` `public static DateTime FormatDateTime(string dateTime) { DateTime _createdAt = new DateTime(); // 2012-09-14T04:42:25.117Z DateTime.TryParse(dateTime, out _createdAt); return _createdAt; }` gives me these two lines `"2012-08-17T04:39:52.878Z"` `1/1/0001 12:00:00 AM` – cdownard Feb 06 '13 at 05:23
  • The same result occurs when using TryParseExact as in the following `DateTime.TryParseExact(dateTime, TIMEFORMATTER, CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal, out _createdAt);` This problem is odd to me but I must just be doing something simple that's incorrect. – cdownard Feb 06 '13 at 05:33
  • I just tested "2012-17-08T04:39:52.878Z" with `string d = "2012-17-08T04:39:52.878Z"; DateTime date = new DateTime(); DateTime.TryParse(d, out date); Debug.WriteLine(date.ToString());` and I get the same 1/1/0001 12:00:00 AM date back. – cdownard Feb 06 '13 at 05:39