jQuery.Extensions.js for ParseJSON Dates
jQuery Extension Library This library is used to extend jQuery to convert JSON Dates (ASP.NET and ISO) to Javascript Dates
by cjgaudin
JavaScript
/*!
* jQuery.parseJSON() extension (supports ISO & Asp.net date conversion)
Make sure to add the following to jQuery:
$.ajaxSetup({
// Add a custom converter to convert ASP.NET JSON dates to JS Date object
// - must use jquery.extensions.js to extend $.parseJSON to convert dates
converters: {
"text json": function (data) {
//Need to use the
return $.parseJSON(data, true);
}
}
});
*/
(function ($) {
// JSON RegExp
var rvalidchars = /^[\],:{}\s]*$/;
var rvalidescape = /\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g;
var rvalidtokens = /"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g;
var rvalidbraces = /(?:^|:|,)(?:\s*\[)+/g;
var dateISO = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2}(?:\.\d*)?)(?:([\+-])(\d{2})\:(\d{2}))?Z?$/; ///\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:[.,]\d+)?Z/i;
var dateNet = /\/Date\((\d+)(?:-\d+)?\)\//i;
// replacer RegExp - added /g to end to replace all occurances in the string
var replaceISO = /"(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})(?:[.,](\d+))?Z?"/g;
var replaceNet = /"\\\/Date\((\d+)(?:-\d+)?\)\\\/"/g;
// determine JSON native support
var nativeJSON = (window.JSON && window.JSON.parse) ? true : false;
var extendedJSON = nativeJSON && window.JSON.parse('{"x":9}', function (k, v) { return "Y"; }) === "Y";
var jsonDateConverter = function (key, value) {
if (typeof (value) === "string") {
if (dateISO.test(value)) {
//Added to parse JSON.NET ISO Dates with its milliseconds lower than it expects
var a = dateISO.exec(value);
if(a) {
var utcMilliseconds = Date.UTC(+a[1], +a[2] - 1, +a[3], +a[4], +a[5], +a[6]);
return new Date(utcMilliseconds);
}
else
return new Date(value);
}
if (dateNet.test(value)) {
return...