gerDate() {
function addNum (str) {
if(parseInt(str) > 9) {
return str
} else {
return '0'+str;
}
};
let newDate = new Date();
let Y = newDate.getFullYear();
let M = newDate.getMonth()+1;
let D = newDate.getDate();
let H = newDate.getHours()+8; //获取的是格林尼治时间 +8 为北京时间
let m = newDate.getMinutes();
let S = newDate.getSeconds();
return `${Y}-${addNum(M)}-${addNum(D)} ${addNum(H)}:${addNum(m)}:${addNum(S)}`;
}
2: 格式: 2017-01-02 12:20:20.151
new Date(时间).toISOString().replace('T', ' ').replace('Z', '');
1其他格式 谷歌浏览器
new Date().toDateString() -> "Mon May 22 2017"
new Date().toTimeString() -> "17:56:32 GMT+0800 (中国标准时间)"
new Date().toLocaleDateString() -> "2017-5-22"
new Date().toLocaleTimeString() ->"17:58:13"
new Date().toUTCString() -> "Mon, 22 May 2017 09:59:03 GMT"
new Date().getMilliseconds() -> '获取毫秒数', '386'
new Date().toLocaleString() -> "2017-5-31 17:38:00"
`${new Date().toLocaleString()}:${new Date().getMilliseconds()}` -> "2017-5-31 17:40:48:361"
比较日期
直接比较
var now1 = new Date();
var now2 = new Date();
now1 > now2
//false
now1 < now2
//true