# 根据常见三种日期格式返回距 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()); // 当前时间
本文来自博客园,作者:懒惰ing,转载请注明原文链接:https://www.cnblogs.com/landuo629/p/13292585.html

 
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号