json转换时间格式为一般格式
网上有好多说json传过来的时间格式是 /Date(1436595149269)/ 这种类型
其中较为简单实用的方法如下:
<script>
//yyyy-MM-dd HH:mm:SS
function getDateTime(date) {
var year = date.getFullYear();
var month = date.getMonth() + 1;
var day = date.getDate();
var hh = date.getHours();
var mm = date.getMinutes();
var ss = date.getSeconds();
return year + "-" + month + "-" + day + " " + hh + ":" + mm + ":" + ss;
}
//调用的是这个方法
function ConvertJSONDateToJSDate(jsondate) {
var date = new Date(parseInt(jsondate.replace("/Date(", "").replace(")/", ""), 10));
return date;
}
var date = "/Date(1379944571737)/";
alert(getDateTime(ConvertJSONDateToJSDate(date)));
</script>
- 而我遇到的问题是:
Object { szphid: 36, szphtitle: "213", szphauthor: "123321", szphdate: 1508969100000, szphdw: "中建路桥集团有限公司", szphaddress: "就业报告厅", szphcontent: "<p>1233213</p> " }
其中时间为szphdate: 1508969100000,这应该是后台传回的毫秒数,我想让它变成HH:mm格式
但是date.getHours()和date.getMinutes()的返回值如果<10,将会返回一位的小时数和分数,所以我加了一个简单的方法实现无论何时,返回值都是两位:
function getDateTime(date) {
var hh = date.getHours();
hh = checkTime(hh)
var mm = date.getMinutes();
//var ss = date.getSeconds();
mm = checkTime(mm)
console.log(hh+":"+mm)
return hh + ":" + mm ;
}
/* date.getHours();
* date.getMinutes();
*当hh 和 mm < 10时也显示两位数字
*
* */
function checkTime(i)
{
if (i<10)
{i="0" + i}
return i
}
调用方法为:
var date =1508969100000;
date=new Date(parseInt(date))
date = getDateTime(date)
//console.log(date)
结果是:
06:05