ASP.NET Parse DateTime从ajax调用到javascript日期的结果 [英] ASP.NET Parse DateTime result from ajax call to javascript date

查看:154
本文介绍了ASP.NET Parse DateTime从ajax调用到javascript日期的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简介:

我的ASP.NET页面上有一个 WebMethod 返回 Person 对象。
其中一个字段是生日,这是 DateTime 属性。

I have a WebMethod on my ASP.NET page which returns a Person object. One of the fields is Birthday which is a DateTime property.

WebMethod

WebMethod

[WebMethod]
public static Person GetPerson()
{
     Person p = new Person() {
         Id = 1,
         Name = "Test",
         Birthday = new DateTime(1988, 9, 13)
     };

     return p;
}

如果我使用 $ .ajax拨打电话( )我通过 Person 对象获得服务器的响应。

If I make the call using $.ajax() I get the response of the server with the Person object.

Ajax调用

Ajax call

// Class instance
var Ajaxcalls = function () {

}

_$.extend(Ajaxcalls, {
    GetPerson: function (label) {
        var self = label instanceof _$ ? label : $(label);

        _$.ajax({
            url: 'Default.aspx/GetPerson',
            type: "POST",
            dataType: "json",
            contentType: "application/json; charset=utf-8",
            success: function (data) {
                console.log(JSON.stringify(data.d));
                self.html(new Date(Date.parse(data.d.Birthday)));
            }
        });
    }
});

结果:

Result:

{"__type":"AjaxTest.Classes.Person","Id":1,"Name":"Test","Birthday":"/Date(590104800000)/"}

问题

如何将生日 [/日期(590104800000)/]解析为javascript / jQuery日期?
我试过新日期(Date.parse(data.d.Birthday))但它给了我一个无效日期

How do I parse the Birthday [/Date(590104800000)/] to a javascript/jQuery date? I tried new Date(Date.parse(data.d.Birthday)) but it gives me an Invalid date.

推荐答案

使用 ToJavaScriptDate()执行此操作的函数为你:

Use ToJavaScriptDate() function that does this for you:

function ToJavaScriptDate(value) {
  var pattern = /Date\(([^)]+)\)/;
  var results = pattern.exec(value);
  var dt = new Date(parseFloat(results[1]));
  return (dt.getMonth() + 1) + "/" + dt.getDate() + "/" + dt.getFullYear();
}

ToJavaScriptDate()函数接受 \ / Date(ticks)\ / 格式的值,并以 MM / dd / yyyy format。

内部, ToJavaScriptDate()函数使用表示模式的正则表达式 / Date \\ \\(([^)] +)\)/

exec()方法接受源日期值和测试值的匹配。 exec()的返回值是一个数组。在这种情况下,结果数组的第二个元素(结果[1] )保存源日期的刻度部分。

The ToJavaScriptDate() function accepts a value in \/Date(ticks)\/ format and returns a date string in MM/dd/yyyy format.
Inside, the ToJavaScriptDate() function uses a regular expression that represents a pattern /Date\(([^)]+)\)/.
The exec() method accepts the source date value and tests for a match in the value. The return value of exec() is an array. In this case the second element of the results array (results[1]) holds the ticks part of the source date.

例如,如果源值为 \ /日期(836418600000)\ / ,则结果[1] 836418600000

根据此刻度值,形成一个JavaScript Date对象。 Date对象有一个构造函数,它接受自1970年1月1日以来的毫秒数。

因此 dt 包含一个有效的JavaScript Date对象。

ToJavaScriptDate()函数然后将日期格式化为 MM / dd / yyyy 并返回给调用者。

For example, if the source value is \/Date(836418600000)\/ then results[1] will be 836418600000.
Based on this ticks value a JavaScript Date object is formed. The Date object has a constructor that accepts the number of milliseconds since 1 January 1970.
Thus dt holds a valid JavaScript Date object.
The ToJavaScriptDate() function then formats the date as MM/dd/yyyy and returns to the caller.

您可以使用 ToJavaScriptDate()函数,如下所示:

You can use the ToJavaScriptDate() function as shown below:

options.success = function (order) {
 alert("Required Date : " + ToJavaScriptDate(order.RequiredDate) + ", Shipped Date : " + ToJavaScriptDate(order.ShippedDate));
};

虽然上面的例子使用中的日期MM / dd / yyyy 格式,一旦构造了Date对象,你就可以自由地使用任何其他格式。

Although the above example uses date in MM/dd/yyyy format, you are free to use any other format once a Date object is constructed.

reference:链接

reference : Link

这篇关于ASP.NET Parse DateTime从ajax调用到javascript日期的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆