如何格式化微软JSON日期? [英] How to format a Microsoft JSON date?

查看:318
本文介绍了如何格式化微软JSON日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以我的第一次尝试在阿贾克斯与jQuery。我得到我的数据到我的网页,但我有一些麻烦时返回的日期数据类型JSON数据。基本上,我得到一个字符串返回,看起来像这样:

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

这是有人全新的以JSON - 我如何格式化这个为短日期格式?如果这是jQuery的code的地方处理?我试着使用 jQuery.UI.datepicker 插件 $。datepicker.formatDate()没有任何成功。

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:这是我想出了使用的答案组合这里的解决方案:

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.

推荐答案

评估是没有必要的。这将正常工作:

Eval is not necessary. This will work fine:

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

SUBSTR函数取出/日期(部分,而parseInt函数函数获取整数并忽略)/结尾。得到的号码被传递到Date构造函数。

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.

编辑:我故意冷落的基数(第二个参数parseInt函数);看<一个href=\"http://stackoverflow.com/questions/206384/how-to-format-a-microsoft-json-date#comment19286516_2316066\">my下面评论。此外,我完全<一个同意href=\"http://stackoverflow.com/questions/206384/how-to-format-a-microsoft-json-date#comment20315450_2316066\">Rory's评论:ISO-8601日期pferred这件旧格式$ P $ - 所以这种格式通常不应该被用于新的开发。看到优秀的 Json.NET 库序列化使用ISO-8601格式的日期一个伟大的选择。

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.

有关ISO-8601格式的JSON日期,只是通过串入Date构造函数:

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

这篇关于如何格式化微软JSON日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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