//这种方法的出处找不到了,时间太久了,谁知道的话可以贴出来,膜拜大神
getCurrentTime("yyyy-MM-dd hh:mm:ss");
getCurrentTime: function(fmt){
var currentDate = new Date();
var o = {
"M+": currentDate.getMonth() + 1, // 月份
"d+": currentDate.getDate(), // 日
"h+": currentDate.getHours(), // 小时
"m+": currentDate.getMinutes(), // 分
"s+": currentDate.getSeconds(), // 秒
"q+": Math.floor((currentDate.getMonth() + 3) / 3), // 季度
"S": currentDate.getMilliseconds() // 毫秒
};
if (/(y+)/.test(fmt))
fmt = fmt.replace(RegExp.$1, (currentDate.getFullYear() + "").substr(4 - RegExp.$1.length));
for (var k in o)
if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
return fmt;
}
新发现的,出处地址:https://www.jianshu.com/p/0e97aca535c0
//旧方法比较见的
function transformTime(timestamp = +new Date()) {
if (timestamp) {
var time = new Date(timestamp);
var y = time.getFullYear();
var M = time.getMonth() + 1;
var d = time.getDate();
var h = time.getHours();
var m = time.getMinutes();
var s = time.getSeconds();
return y + '-' + addZero(M) + '-' + addZero(d) + ' ' + addZero(h) + ':' + addZero(m) + ':' + addZero(s);
} else {
return '';
}
}
function addZero(m) {
return m < 10 ? '0' + m : m;
}
transformTime(); // "2018-08-08 12:09:12"
新方法
function time(time = +new Date()) {
var date = new Date(time + 8 * 3600 * 1000); // 增加8小时
return date.toJSON().substr(0, 19).replace('T', ' ');
}
time(); // "2018-08-09 18:25:54"