Skip to content

Instantly share code, notes, and snippets.

@smiler
Created June 21, 2012 12:13
Show Gist options
  • Save smiler/2965425 to your computer and use it in GitHub Desktop.
Save smiler/2965425 to your computer and use it in GitHub Desktop.
// Add an jQuery AJAX converter to remove fix formatting of dates returned from ASP.NET web methods.
// See http://weblogs.asp.net/bleroy/archive/2008/01/18/dates-and-json.aspx for more information.
(function () {
var DATE_START = "/Date(";
var DATE_START_LENGTH = DATE_START.length;
function startsWith(str, needle) {
return (str.substr(0, needle.length) === needle);
}
function isDateString(s) {
return typeof s === "string" && startsWith(s, DATE_START);
}
function deserializeDateString(dateString) {
return new Date(parseInt(dateString.substr(DATE_START_LENGTH)));
}
function convertJSONDates(key, value) {
if (isDateString(value)) {
return deserializeDateString(value);
}
return value;
}
window.jQuery.ajaxSetup({
converters: {
"text json": function (data) {
return window.JSON.parse(data, convertJSONDates); // window.JSON is provided by json2.js for old browsers
}
}
});
} ());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment