您的位置:首页 > Web前端 > JavaScript

JSON日期格式解决方案

2012-04-14 13:41 330 查看
如果Json返回的日期格式是:/Date(1199116800000)/ 这种形式,其原因是服务器端使用的数据类型为DateTime类型,如果使用String类型则是正常的时间格式,

当然也可以在JS中处理,方法有挺多种,例如:

<script language="javascript" type="text/javascript">
$(function () {

var date= "/Date(1199116800000)/";

alert(DateFormat(date)); //调用

});

function DateFormat(value) {

var date = new Date(parseInt(value.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();

var Hours = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();

var Minutes = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();

var Seconds = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();

return date.getFullYear() + "/" + month + "/" + currentDate + " " + Hours + ":" + Minutes + ":" + Seconds;

}

</script>

其实只是取整数部部分,如:/Date(1199116800000)/ 取 1199116800000 ,然后使用 var date = new Date(1199116800000) ;

然后date.Month() 之后就是拼字符串了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: