Skip to content

Instantly share code, notes, and snippets.

@kristofferh
Created March 16, 2011 17:39
Show Gist options
  • Save kristofferh/872902 to your computer and use it in GitHub Desktop.
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
/**
* 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)
@dandean
Copy link

dandean commented Mar 16, 2011

.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:

function jsonDate(str) {
    return new Date(1 * str.match(/\d+/)[0]);
}

@dandean
Copy link

dandean commented Mar 16, 2011

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"))

@dandean
Copy link

dandean commented Mar 16, 2011

Also,

blah blah blah blah blah.

Dan

@kristofferh
Copy link
Author

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