即日起在codingBlog上分享您的技术经验即可获得积分,积分可兑换现金哦。

如何格式化微软JSON日期?

栈溢出 wangfei 340℃ 0评论
本文目录
[隐藏]

1.原始问题:How do I format a Microsoft JSON date?

I’m taking my first crack at Ajax with jQuery. I’m getting my data onto my page, but I’m having some trouble with the JSON data that is returned for Date data types. Basically, I’m getting a string back that looks like this:

/Date(1224043200000)/

From someone totally new to JSON – How do I format this to a short date format? Should this be handled somewhere in the jQuery code? I’ve tried the jQuery.UI.datepicker plugin using $.datepicker.formatDate() without any success.

FYI: Here’s the solution I came up with using a combination of the answers here:

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

This solution got my object from the callback method and displayed the dates on the page properly using the date format library.

2.被采纳答案

Eval is not necessary. This will work fine:

var date = new Date(parseInt(jsonDate.substr(6)));

The substr function takes out the “/Date(” part, and the parseInt function gets the integer and ignores the “)/” at the end. The resulting number is passed into the Date constructor.

EDIT: I have intentionally left out the radix (the 2nd argument to parseInt); see my comment below. Also, I completely agree with Rory’s comment: ISO-8601 dates are preferred over this old format — so this format generally shouldn’t be used for new development. See the excellent Json.NET library for a great alternative that serializes dates using the ISO-8601 format.

For ISO-8601 formatted JSON dates, just pass the string into the Date constructor:

var date = new Date(jsonDate); //no ugly parsing needed; full timezone support

3.其他高票答案

3.1.第1个答案

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.

3.2.第2个答案

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.

3.3.第3个答案

You can use this to get a date from JSON:

var date = eval(jsonDate.replace(/\/Date\((\d+)\)\//gi, "new Date($1)"));

And then you can use a JavaScript Date Format script (1.2 KB when minified and gzipped) to display it as you want.

3.4.第4个答案

Check up the date ISO standard; kind of like this:

yyyy.MM.ddThh:mm

It becomes 2008.11.20T22:18.

转载请注明:CodingBlog » 如何格式化微软JSON日期?

喜欢 (0)or分享 (0)
发表我的评论
取消评论

*

表情
(1)个小伙伴在吐槽
  1. I still can't believe that such a small gift made me so happy. I was excited like a kid...you should https://www.etsy.com/listing/532018440/garden-ant-playing-guitar - check it here
    匿名2017-11-22 11:08 回复