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

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


当前回答

在页面中添加jQuery UI插件:

function DateFormate(dateConvert) {
    return $.datepicker.formatDate("dd/MM/yyyy", eval('new ' + dateConvert.slice(1, -1)));
};

其他回答

试试这个。。。

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(

一篇迟来的帖子,但对那些搜索过这篇帖子的人来说。

想象一下:

    [Authorize(Roles = "Administrator")]
    [Authorize(Roles = "Director")]
    [Authorize(Roles = "Human Resources")]
    [HttpGet]
    public ActionResult GetUserData(string UserIdGuidKey)
    {
        if (UserIdGuidKey!= null)
        {
            var guidUserId = new Guid(UserIdGuidKey);
            var memuser = Membership.GetUser(guidUserId);
            var profileuser = Profile.GetUserProfile(memuser.UserName);
            var list = new {
                              UserName = memuser.UserName,
                              Email = memuser.Email ,
                              IsApproved = memuser.IsApproved.ToString() ,
                              IsLockedOut = memuser.IsLockedOut.ToString() ,
                              LastLockoutDate = memuser.LastLockoutDate.ToString() ,
                              CreationDate = memuser.CreationDate.ToString() ,
                              LastLoginDate = memuser.LastLoginDate.ToString() ,
                              LastActivityDate = memuser.LastActivityDate.ToString() ,
                              LastPasswordChangedDate = memuser.LastPasswordChangedDate.ToString() ,
                              IsOnline = memuser.IsOnline.ToString() ,
                              FirstName = profileuser.FirstName ,
                              LastName = profileuser.LastName ,
                              NickName = profileuser.NickName ,
                              BirthDate = profileuser.BirthDate.ToString() ,
            };
            return Json(list, JsonRequestBehavior.AllowGet);
        }
        return Redirect("Index");
    }

如您所见,我正在利用C#3.0的特性创建“Auto”泛型。它有点懒,但我喜欢它,而且很管用。请注意:Profile是我为web应用程序项目创建的自定义类。

简单地使用Moment.js。它非常简单

var finalDate = moment("/Date(1198908717056)/").format("dddd, MMM Do YYYY, hh:mm:ss a");

//"Saturday, Dec 29th 2007, 12:11:57 pm"


var finalDate2 = moment("/Date(1198908717056)/").format("DD-MMM-YYYY");  //"29-Dec-2007"

我最终将“字符”添加到Panos的正则表达式中,以消除Microsoft序列化程序在将对象写入内联脚本时生成的字符:

所以,如果你的C#代码中有一个属性

protected string JsonObject { get { return jsSerialiser.Serialize(_myObject); }}

在你的aspx中

<script type="text/javascript">
    var myObject = '<%= JsonObject %>';
</script>

你会得到类似的东西

var myObject = '{"StartDate":"\/Date(1255131630400)\/"}';

注意双引号。

为了将其转换为eval将正确反序列化的形式,我使用了:

myObject = myObject.replace(/"\/Date\((\d+)\)\/"/g, 'new Date($1)');

我使用原型,为了使用它,我添加了

String.prototype.evalJSONWithDates = function() {
    var jsonWithDates = this.replace(/"\/Date\((\d+)\)\/"/g, 'new Date($1)');
    return jsonWithDates.evalJSON(true);
}

使用jQuery UI日期选择器-只有在已经包含jQuery UI的情况下才有意义:

$.datepicker.formatDate('MM d, yy', new Date(parseInt('/Date(1224043200000)/'.substr(6)))); 

输出:

2008年10月15日