// 根據毫秒數構建 Date 對象
var date = new Date(1499996760000);
// 格式化日期
dateTime = date.toLocaleString();
這時候 dateTime 的值為"2018/07/10 下午2:07:02"。可以通過重寫 toLocaleString() 方法,來自定義日期顯示格式。
// 重寫方法,自定義格式化日期
Date.prototype.toLocaleString = function() {
// 補0 例如 2018/7/10 14:7:2 補完后為 2018/07/10 14:07:02
function addZero(num) {
if(num<10)
return "0" + num;
return num;
}
// 按自定義拼接格式返回
return this.getFullYear() + "/" + addZero(this.getMonth() + 1) + "/" + addZero(this.getDate()) + " " +
addZero(this.getHours()) + ":" + addZero(this.getMinutes()) + ":" + addZero(this.getSeconds());
};
// 根據毫秒數構建 Date 對象
var date = new Date(1499996760000);
// 按重寫的自定義格式,格式化日期
dateTime = date.toLocaleString();
---------------------
作者:cherlshall
來源:CSDN
原文:https://blog.csdn.net/cherlshall/article/details/80985209
版權聲明:本文為博主原創文章,轉載請附上博文鏈接!