1、日期转换时间戳
①、 Date.parse();
var time = Date.parse(new Date('2021-3-5'));
console.log(time); //1614873600000
②、valueOf() (获取到秒)
var time = (new Date('2021-3-5')).valueOf();
console.log(time); //1614873600000
③、getTime() (获取到秒)
var time = (new Date('2021-3-5')).getTime();
console.log(time);//1614873600000
④、Date.now() (获取时间戳 到秒)
console.log(Date.now())
console.log(Date.now('2021-4-01'))
2、时间戳转为日期
①、转换为年月日
var year_month = (new Date('2021-3-5')).toLocaleDateString();
console.log(year_month); // 2021/3/5
year_month.replace(/\//g,'-');
console.log(year_month); // 2021-3-5
②、时间戳转为年月日
var s = 1614873600000;//得到时间戳
console.log(new Date(s).toLocaleString()); //2021/3/5 上午12:00:00
3、两个时间戳之差 获取天数
var start=1491789600000;//2017-4-10 10:00
var end=1494381600000;//2017-5-10 10:00
var utc=end-start;
utc/(24*60*60*1000);// 天
utc/(60*60*1000);//小时
utc/(60*1000); // 分