Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
504 views
in Technique[技术] by (71.8m points)

c# - ASP.NET Parse DateTime result from ajax call to javascript date

Introduction:

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]
public static Person GetPerson()
{
     Person p = new Person() {
         Id = 1,
         Name = "Test",
         Birthday = new DateTime(1988, 9, 13)
     };

     return p;
}

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

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)/"}

Problem

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Use convertToJavaScriptDate() function that does this for you:

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

The convertToJavaScriptDate() function accepts a value in /Date(ticks)/ format and returns a date string in MM/dd/yyyy format.
Inside, the convertToJavaScriptDate() 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.

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 convertToJavaScriptDate() function then formats the date as MM/dd/yyyy and returns to the caller.

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

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

Although the above example uses date in MM/dd/yyyy format, you can use other formats also once Date object is constructed.

reference : Link


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...