Json日期格式的问题

用JQuery Ajax返回一个Entity的Json数据时,如果Entity的属性中有日期格式,那返回来的是一串字符串,如下图所示:

在网上找了很久也沒有找到一个好的解決方案,最后自己写一個javascrip Function 特此记录,以备后用:

UPDATE:可以用Json.net中IsoDateTimeConverter:

?
Product pro = new  Product();
          pro.PNo = "001" ;
          pro.PName = "Apple" ;
          pro.PDate = new  DateTime(2011, 12, 28);
          pro.Price = 3.99M;
 
 
          IsoDateTimeConverter timeConverter = new  IsoDateTimeConverter(); //这里使用自定义日期格式,默认是ISO8601格式        
            timeConverter.DateTimeFormat = "yyyy-MM-dd" ; //设置时间格式
 
          string  json = JsonConvert.SerializeObject(pro,Formatting.Indented,timeConverter); //转换序列化的对象
            //string json = JsonConvert.SerializeObject(pro, new IsoDateTimeConverter());//默认是ISO8601格式
           context.Response.Write(json);}

  输出结果:"{\r\n  \"PNo\": \"001\",\r\n  \"PName\": \"Apple\",\r\n  \"PDate\": \"2011-12-28\",\r\n  \"Price\": 3.99\r\n}"

调用前的图像问题:

 

 
?
function ChangeDateFormat(cellval)
  {
      var date = new  Date(parseInt(cellval.replace( "/Date(" , "" ).replace( ")/" , "" ), 10));
      var month = date.getMonth() + 1 < 10 ? "0"  + (date.getMonth() + 1) : date.getMonth() + 1;
      var currentDate = date.getDate() < 10 ? "0"  + date.getDate() : date.getDate();
      return  date.getFullYear() + "-"  + month + "-"  + currentDate;
  }

  

?
function  DataBindToMemberDetailDialog(memberObj)
{
      $( "#span_memberName" ).text(memberObj.UserName);
      $( "#span_sex" ).text(memberObj.SexText);
      $( "#span_firstNameAndLastName" ).text(memberObj.LastName + " "  + memberObj.FirstName);
      $( "#span_birthday" ).text(ChangeDateFormat(memberObj.Birthday));
      $( "#span_email" ).text(memberObj.Email);
      $( "#span_memberRate" ).text( "此處還要修改" );
      $( "#span_loginCount" ).text(memberObj.LoginCount);
     $( "#span_downloadCount" ).text(memberObj.DownloadCouponCount);
     $( "#span_scoreCount" ).text(memberObj.ScoreCount);
     $( "#span_createDate" ).text(memberObj.CreateDate);
}

  

执行后的结果:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值