62,047
社区成员
发帖
与我相关
我的任务
分享
String.prototype.toDateTime = function () {
var dateMilliseconds;
if (isNaN(this)) {
//使用正则表达式将日期属性中的非数字(\D)删除
dateMilliseconds = this.replace(/\D/igm, "");
} else {
dateMilliseconds = this;
}
var date = new Date(parseInt(dateMilliseconds));
//实例化一个新的日期格式,使用1970 年 1 月 1 日至今的毫秒数为参数
//return new Date(parseInt(dateMilliseconds));
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;
};
使用直接
xxx.toDateTime就可以了