Created
March 16, 2011 17:39
-
-
Save kristofferh/872902 to your computer and use it in GitHub Desktop.
Convert a jsonDate (something like /Date(1284604200000-0700)/, .NET likes this) to something more human readable
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* convert a JSONDATE (something like /Date(1284604200000-0700)/) to something more human readable | |
*/ | |
function jsonDate(str) { | |
return new Date(parseInt(str.replace(/\/Date\((.*?)\)\//gi, "$1"), 10)); | |
} | |
// jsonDate('/Date(1284604200000-0700)/'); | |
// -> Wed Sep 15 2010 19:30:00 GMT-0700 (PDT) | |
By the way, the correct format to serialize a date is something like this:
"2011-03-16T18:52:38.084Z"
Which is generated with something like: JSON.stringify(myDate)
Which can be turned back into a date like this:
var date = new Date(Date.parse("2011-03-16T18:52:38.084Z"))
Also,
blah blah blah blah blah.
Dan
You're right, parseInt is tossing the '-0700'. I guess I should have checked that.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
.NET wraps serialized dates with "Date(...)" ? That's so stupid!
I just did a quick test, and parseInt is throwing the "-0700" away, so this might be a little more clear: