0

I have a date in the following format:

2012-12-09T02:08:34.6225152Z

I'm using the datejs javascript library and would like the parse the above date, but I can't seem to figure out the proper format string. I've tried the following, but it doesn't work.

Date.parse('2012-12-09T02:08:34.6225152Z', 'yyyy-MM-ddTHH:mm:ss.fffffffZ');

If it's easier, I'm also open to parsing the string in native javascript.

2 Answers 2

1

DateJS doesn't seems to support milliseconds parsing. There's the u FormatSpecifier on the DateJS extras that could work (haven't tested it).

http://code.google.com/p/datejs/wiki/FormatSpecifiers

Sign up to request clarification or add additional context in comments.

1 Comment

Unfortunately this doesn't work. The DateJs extras stuff is only for output formatting (i.e. toString()), not parsing.
0

I ended up solving this problem by changing the format of the string as it was sent to Javascript. Instead of 2012-12-09T02:08:34.6225152Z I changed the JSON serializer to output 1363607010099 by using the following code:

var serializer = new JsonNetSerializer(new JsonSerializerSettings
{
    DateFormatHandling = DateFormatHandling.MicrosoftDateFormat,
    NullValueHandling = NullValueHandling.Ignore
});

I then changed my javascript to parse the date using this function that I found:

String.prototype.toDate = function () {
    "use strict";
    var match = /\/Date\((\d{13})\)\//.exec(this);
    return match === null ? null : new Date(parseInt(match[1], 10));
};

Finally, I output the date using this bit of code (taking advantage of the DateJS library):

myTime.toDate().toString('h:mm:ss tt dd-MMM-yyyy')

And it works. This seems a bit hackish, so I'm more than open to consider alternatives.

2 Comments

Constructive suggestion: Its a bit ugly to extend the string-object with the toDate function, because not all strings can be converted to a date. Why should that function be available to all possible strings, while you only need it for one specific string only? "some".toDate() makes no sense, while ""today".toDate() or "2013-1-1".toDate() will make sense but returns null. -- just supporting :)
Good point. I'm not suggesting that my solution is a good one, but I saw that the question was still open and wanted to close it with an accepted answer which was the way I ended up solving it. If you have a better alternative to solve the original question, then please post your answer. =)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.