# 根据常见三种日期格式返回距 1970 年 1 月 1 日之间的毫秒数

YYYY

const a = new Date('2012'); 
console.log(a); //Sun Jan 01 2012 08:00:00 GMT+0800 (中国标准时间)

YYYY-MM

const b = new Date('2012-12'); //也可以写成 '2012/12'
console.log(b); //Sat Dec 01 2012 08:00:00 GMT+0800 (中国标准时间)

YYYY-MM-DD

const c = new Date('2012-12-21'); //也可以写成 '2012/12/21'
console.log(c); //Fri Dec 21 2012 08:00:00 GMT+0800 (中国标准时间)
  • 括号内的值必须是字符串

返回距 1970 年 1 月 1 日之间的毫秒数

console.log(a.getTime()); //1325376000000
console.log(b.getTime()); //1354320000000
console.log(c.getTime()); //1356048000000
  • 另一种更加简便的方法
const c = new Date('2012-12-21'); //也可以写成 '2012/12/21'
console.log(c * 1); //1356048000000
console.log(Date.now()); // 当前时间
posted @ 2020-07-13 12:31  懒惰ing  阅读(454)  评论(0)    收藏  举报