我将用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;
}

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


当前回答

只是在这里添加另一种方法,WCF采用的“滴答方法”如果您不非常小心,如这里和其他地方所述,则很容易出现时区问题。所以我现在使用的是.NET和JavaScript都适当支持的ISO 8601格式,其中包括时区偏移。详情如下:

在WCF/.NET中:

其中CreationDate是System.DateTime;ToString(“o”)使用.NET的往返格式说明符,该说明符生成符合ISO 8601的日期字符串

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

在JavaScript中

在检索JSON之后,我使用接受ISO 8601日期字符串的Date构造函数将日期修正为JavaSriptDate对象。。。

$.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());
    }
)

一旦有了JavaScript日期,就可以使用所有方便可靠的date方法,如toDateString、toLocaleString等。

其他回答

如果在JavaScript中,

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

您将看到这是正确的日期,您可以在JavaScript代码中的任何地方使用它与任何框架。

如果.NET返回。。。

return DateTime.Now.ToString("u"); //"2013-09-17 15:18:53Z"

然后在JavaScript中。。。

var x = new Date("2013-09-17 15:18:53Z");

我使用这个简单的函数从Microsoft JSON date获取日期

function getDateValue(dateVal) {
    return new Date(parseInt(dateVal.replace(/\D+/g, '')));
};

replace(/\D+/g,“”)将删除除数字以外的所有字符

parseInt将字符串转换为数字

用法

$scope.ReturnDate = getDateValue(result.JSONDateVariable)

试试这个。。。

function formatJSONDate(jsonDate) {
            var date = jsonDate;
            var parsedDate = new Date(parseInt(date.toString().substring(6)));
            var newDate = new Date(parsedDate);
            var getMonth = newDate.getMonth() + 1;
            var getDay = newDate.getDay();
            var getYear = newDate.getFullYear(); 

            var standardDate = (getMonth<10 ? '0' : '') + getMonth + '/' + (getDay<10 ? '0' : '') + getDay + '/' + getYear;
            return standardDate;
        }

getYear()返回1900年,这已经被弃用一段时间了,最好使用getFullYear(

我的约会是这样的:

"/Date(1276290000000+0300)/"

在某些示例中,日期的格式略有不同:

"/Date(12762900000000300)/"
"Date(1276290000000-0300)"

etc.

所以我提出了以下RegExp:

/\/+Date\(([\d+]+)\)\/+/

最终代码为:

var myDate = new Date(parseInt(jsonWcfDate.replace(/\/+Date\(([\d+-]+)\)\/+/, '$1')));

希望有帮助。

更新:我从Microsoft找到了此链接:如何使用JSON序列化日期?

这似乎是我们都在寻找的。