简体   繁体   English

将 .NET DateTime 对象转换为 Javascript Date 对象

[英]converting .NET DateTime object to Javascript Date object

I've the following problem:我有以下问题:

I retrieve a DateTime object from SQL Server and pass it via JSON (using $.ajax) to Javascript.我从 SQL Server 检索一个 DateTime 对象并通过 JSON(使用 $.ajax)将它传递给 Javascript。 I have experienced difficulty trying to convert the retrieved object to a Date object in javascript.我在尝试将检索到的对象转换为 javascript 中的 Date 对象时遇到了困难。 The retrieved object is a string of value "/Date(615592800000)/".检索到的对象是一串值“/Date(615592800000)/”。 I think the value is an epoch time.我认为价值是划时代的。

My question is, is there another way of retrieving the date object than to use regex to select the epoch value and then create a new Date object?我的问题是,除了使用正则表达式选择纪元值然后创建一个新的 Date 对象之外,还有另一种检索日期对象的方法吗?

I'm fairly new to JS, so any help would be appreciated.我对 JS 相当陌生,所以任何帮助将不胜感激。

not that I know... this is the function i'm using, just in case ...不是我知道......这是我正在使用的功能,以防万一......

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

This is because JSON as standard does not have a DateTime format - vendors are free to mark it down as they want.这是因为 JSON 作为标准没有 DateTime 格式 - 供应商可以根据需要自由标记它。 WCF has this weird format of /Date()/ I faced this just a couple of months ago. WCF 有这种奇怪的 /Date()/ 格式,我几个月前就遇到过这种情况。 Using Jquery and Jquery UI it will look like that.使用 Jquery 和 Jquery UI,它看起来像那样。 controlId is the identifier of an element with controlId 是元素的标识符

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

The regex way is the perfectly correct way to go.正则表达式方式是完全正确的方式。

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 } });

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

Try this.尝试这个。 Pass the date string which you get to the below function.将您获得的日期字符串传递给以下函数。 It will give you the JavaScript date object.它将为您提供 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; 
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在asp.net数据表中将日期时间对象转换为仅日期格式 - Converting datetime object to date only format in asp.net datatable 将Apple收据中给定的日期转换为.net DateTime对象 - Converting the date given from Apple receipts to a .net DateTime object asp.net中的DateTime的Javascript序列化没有提供javascript日期对象? - Javascript serialization of DateTime in asp.net is not giving a javascript date object? 在将字符串转换为DateTime对象时 - On converting a string to a DateTime object 将字符串转换为DateTime对象 - Converting a string to DateTime object 在C#中将凌乱的日期字符串转换为dateTime对象 - converting messy date string to dateTime object in c# 将IBM iSeries DB2十进制日期类型转换为DateTime对象 - Converting IBM iSeries DB2 decimal date types into DateTime object InnerException:将字符串转换为DateTime时。 在将每个变量放入DateTime对象之前,分析字符串以对日期进行日期解析 - InnerException:When converting a string to DateTime. parse the string to date the date before putting each variable into the DateTime object 从json转换datetime对象 - converting datetime object from json 将异常的DateTime字符串转换为DateTime对象 - Converting Unusual DateTime String to DateTime Object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM