Jquery 如何格式化 Microsoft JSON 日期?

我正在使用 jQuery 尝试 Ajax。 我正在将我的数据放到我的页面上,但是我在处理为 Date 数据类型返回的 JSON 数据时遇到了一些问题。 基本上,我得到了一个看起来像这样的字符串:

/Date(1224043200000)/

来自对 JSON 完全陌生的人 - 如何将其格式化为短日期格式? 这应该在 jQuery 代码的某个地方处理吗? 我已经使用 $.datepicker.formatDate() 尝试了 jQuery.UI.datepicker 插件,但没有成功。

仅供参考:这是我结合此处的答案提出的解决方案:

function getMismatch(id) {
  $.getJSON("Main.aspx?Callback=GetMismatch",
    { MismatchId: id },
function (result) {
  $("#AuthMerchId").text(result.AuthorizationMerchantId);
  $("#SttlMerchId").text(result.SettlementMerchantId);
  $("#CreateDate").text(formatJSONDate(Date(result.AppendDts)));
  $("#ExpireDate").text(formatJSONDate(Date(result.ExpiresDts)));
  $("#LastUpdate").text(formatJSONDate(Date(result.LastUpdateDts)));
  $("#LastUpdatedBy").text(result.LastUpdateNt);
  $("#ProcessIn").text(result.ProcessIn);
}

);

return false;
}

function formatJSONDate(jsonDate) {
var newDate = dateFormat(jsonDate, “mm/dd/yyyy”);
return newDate;
}

此解决方案从回调方法中获取我的对象,并使用日期格式库在页面上正确显示日期。

Don't repeat yourself - automate date conversion using $.parseJSON()

Answers to your post provide manual date conversion to JavaScript dates. I've extended jQuery's $.parseJSON() just a little bit, so it's able to automatically parse dates when you instruct it to. It processes ASP.NET formatted dates (/Date(12348721342)/) as well as ISO formatted dates (2010-01-01T12.34.56.789Z) that are supported by native JSON functions in browsers (and libraries like json2.js).

Anyway. If you don't want to repeat your date conversion code over and over again I suggest you read this blog post and get the code that will make your life a little easier.

Click here to check the Demo

JavaScript/jQuery

var = MyDate_String_Value = "/Date(1224043200000)/"
var value = new Date
            (
                 parseInt(MyDate_String_Value.replace(/(^.*\()|([+-].*$)/g, ''))
            );
var dat = value.getMonth() +
                         1 +
                       "/" +
           value.getDate() +
                       "/" +
       value.getFullYear();

Result - "10/15/2008"

If you say in JavaScript,

var thedate = new Date(1224043200000);
alert(thedate);

you will see that it's the correct date, and you can use that anywhere in JavaScript code with any framework.

There is no built in date type in JSON. This looks like the number of seconds / milliseconds from some epoch. If you know the epoch you can create the date by adding on the right amount of time.

I also had to search for a solution to this problem and eventually I came across moment.js which is a nice library that can parse this date format and many more.

var d = moment(yourdatestring)

It saved some headache for me so I thought I'd share it with you. :)
You can find some more info about it here: http://momentjs.com/

Don't over-think this. Like we've done for decades, pass a numeric offset from the de-facto standard epoch of 1 Jan 1970 midnight GMT/UTC/&c in number of seconds (or milliseconds) since this epoch. JavaScript likes it, Java likes it, C likes it, and the Internet likes it.

Just to add another approach here, the "ticks approach" that WCF takes is prone to problems with timezones if you're not extremely careful such as described here and in other places. So I'm now using the ISO 8601 format that both .NET & JavaScript duly support that includes timezone offsets. Below are the details:

In WCF/.NET:

Where CreationDate is a System.DateTime; ToString("o") is using .NET's Round-trip format specifier that generates an ISO 8601-compliant date string

new MyInfo {
    CreationDate = r.CreationDate.ToString("o"),
};

In JavaScript

Just after retrieving the JSON I go fixup the dates to be JavaSript Date objects using the Date constructor which accepts an ISO 8601 date string...

$.getJSON(
    "MyRestService.svc/myinfo",
    function (data) {
        $.each(data.myinfos, function (r) {
            this.CreatedOn = new Date(this.CreationDate);
        });
        // Now each myinfo object in the myinfos collection has a CreatedOn field that is a real JavaScript date (with timezone intact).
       alert(data.myinfos[0].CreationDate.toLocaleString());
    }
)

Once you have a JavaScript date you can use all the convenient and reliable Date methods like toDateString, toLocaleString, etc.

var newDate = dateFormat(jsonDate, "mm/dd/yyyy"); 

Is there another option without using the jQuery library?

This may can also help you.

 function ToJavaScriptDate(value) { //To Parse Date from the Returned Parsed Date
        var pattern = /Date\(([^)]+)\)/;
        var results = pattern.exec(value);
        var dt = new Date(parseFloat(results[1]));
        return (dt.getMonth() + 1) + "/" + dt.getDate() + "/" + dt.getFullYear();
    }
</div>