Json日期格式的问题
来源:互联网 发布:平安银行信用卡淘宝联名卡额度 编辑:程序博客网 时间:2024/05/17 17:57
用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);
}
执行后的结果:
- Json日期格式的问题
- json日期格式问题
- 返回json的日期格式问题
- springmvc返回json的日期格式问题
- springmvc学习笔记--json--返回json的日期格式问题
- springmvc学习笔记--json--返回json的日期格式问题
- springmvc学习笔记--json--返回json的日期格式问题
- Json格式日期转换问题
- JQuery返回Json日期格式的问题/Date(1387900800000)/
- 解决JSON序列化日期格式问题
- Spring MVC springmvc学习笔记--json--返回json的日期格式问题
- JSON日期在前端显示的格式
- 通过自定义MVC的Controller的Json转换器解决日期序列化格式问题
- json转换日期格式
- JSON 日期格式转换
- json日期格式转换
- json日期格式转换
- json日期格式转换
- C++程序设计之四书五经
- IOS的一些小知识
- android:layout_weight详解
- 获取局域网计算机名称和IP
- unity3d--javascript的枚举类型写法
- Json日期格式的问题
- 浏览器 CSS hack
- hdu_1241 Oil Deposits
- 应用HttpClient来对付各种顽固的WEB服务器
- this指针
- Android Listener侦听的N种写法
- android NDK开发、编译、调试环境搭建与操作入门
- java时间差
- weakref的使用场景——实现缓存