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

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


当前回答

不要想得太多。像我们几十年来所做的那样,从1970年1月1日午夜GMT/UTC/&c这一事实上的标准时间开始,以秒(或毫秒)为单位传递一个数字偏移量。JavaScript喜欢它,Java喜欢它,C喜欢它,互联网喜欢它。

其他回答

简单地使用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"

eval()不是必需的。这将很好:

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

substr()函数取出/Date(部分),parseInt()函数获取整数并忽略结尾处的)/。生成的数字被传递到Date构造函数中。


我故意省略了基数(parseInt的第二个参数);请看我下面的评论。

此外,我完全同意Rory的评论:ISO-8601日期比这种旧格式更可取,因此这种格式通常不应用于新的开发。

对于ISO-8601格式的JSON日期,只需将字符串传递到Date构造函数:

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

如果您正在使用Kotlin,那么这将解决您的问题。

val dataString = "/Date(1586583441106)/"
val date = Date(Long.parseLong(dataString.substring(6, dataString.length - 2)))

在真棒线程中发布:

var d = new Date(parseInt('/Date(1224043200000)/'.slice(6, -2)));
alert('' + (1 + d.getMonth()) + '/' + d.getDate() + '/' + d.getFullYear().toString().slice(-2));

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

想象一下:

    [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应用程序项目创建的自定义类。