- 首页 > it技术 > >
时间戳转换成24小时格式(2017-01-12 22:03:00)
formatTime(t) {
var timeStr = new Date(t).toLocaleString();
var arr1 = timeStr.split(" ");
var arr2 = arr1[0].split("/");
for(var i = 1;
i <= 2;
i++) {
if(parseInt(arr2[i], 10) < 10) {
arr2[i] = "0" + arr2[i];
}
}
var str1 = arr2.join("-");
var arr3 = arr1[1].split(":");
if(arr3[0].indexOf("上午") !== -1) {
if(parseInt(arr3[0].replace(/上午/g, ""), 10) < 10) {
arr3[0] = "0" + arr3[0].replace(/上午/g, "");
} else {
arr3[0] = arr3[0].replace(/上午/g, "");
}
}
if(arr3[0].indexOf("下午") !== -1) {
arr3[0] = (12 + parseInt(arr3[0].replace(/下午/g, ""), 10)).toString();
}
var str2 = arr3.join(":");
return str1 + " " + str2;
}
推荐阅读