Skip to content

Instantly share code, notes, and snippets.

@andrewdavey
Created September 6, 2010 17:20
Show Gist options
  • Save andrewdavey/567285 to your computer and use it in GitHub Desktop.
Save andrewdavey/567285 to your computer and use it in GitHub Desktop.
jQuery.parseJSON = (function(parse) {
var datePattern = /^\/Date\((\d+)\)\/$/;
return function(data) {
var value = parse(data);
parseDates(value);
return value;
}
function parseDates(obj) {
for (var k in obj) {
if (!obj.hasOwnProperty(k)) continue;
var value = obj[k];
if (typeof value === 'string') {
var match = value.match(datePattern);
if (match) obj[k] = new Date(parseInt(match[1]));
} else if (value instanceof Object) {
parseDates(value);
}
}
}
})(jQuery.parseJSON);
@varInt
Copy link

varInt commented Sep 6, 2010

comment code ? :))

@andrewdavey
Copy link
Author

@varInt feel free to fork if you want to add comments ;)

@varInt
Copy link

varInt commented Sep 6, 2010

show example ..., I do not understand

@andrewdavey
Copy link
Author

jQuery's parseJSON function does not support converting dates represented in "/Date(1234567)/" format into Date objects.
This gist shows how to override the parseJSON function with a new function that recursively looks for date strings in the given format and replaces them with Date objects.

@varInt
Copy link

varInt commented Sep 6, 2010

aw, Ok. Tank you!

nice code :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment