Convert .net DateTime object to a Javascript Date object

I have the following problem:

I am retrieving a DateTime object from SQL Server and passing it through JSON (using $ .ajax) in Javascript. I am having difficulty trying to convert the extracted object to a Date object in javascript. The restored object is a string of values ​​"/ Date (615592800000) /". I think value is an era.

My question is: is there any other way to get a date object than using a regular expression to select an epoch value and then create a new Date object?

I am new to JS, so any help would be appreciated.

+5
source share
4 answers

, ... , , ...

function toDateFromJson(src) {
    return new Date(parseInt(src.substr(6)));
}
+6

, JSON DateTime - . WCF /Date ()/ . JQuery JQuery UI, . controlId -

var converted = eval(original.replace(/\/Date\((\d+)\)\//gi, 'new Date($1)'));
0

- .

var msDateRegex = /"\\\/Date\((-?\d+)\)\\\/"/g;

var msDateJsonConverter = function(data) {
    return JSON.parse($.trim(data.replace(msDateRegex, '{"__date":$1}')), function(key, value) {
        return value && typeof value.__date == "number" ? new Date(value.__date) : value;
    });
};

$.ajaxSetup({ converters: { "text json": msDateJsonConverter } });

: http://weblogs.asp.net/bleroy/archive/2008/01/18/dates-and-json.aspx

0

. , , . JavaScript.

function (val) {
        var reISO = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2}(?:\.\d*)?)Z$/;
        var reMsAjax = /^\/Date\((d|-|.*)\)[\/|\\]$/;


            if (val)) {
                        var a = reISO.exec(val);
                        if (a) {
                            val = new Date(Date.UTC(+a[1], +a[2] - 1, +a[3], +a[4], +a[5], +a[6]));
                            return val;
                        }
                        a = reMsAjax.exec(val);
                        if (a) {
                            var b = a[1].split(/[-+,.]/);
                            val = new Date(b[0] ? +b[0] : 0 - +b[1]);
                            return val;
                        }
                    }

       return val; 
    }
0

All Articles