Js中 new Date()获取的时间转换成时间戳

感谢万能的男友,哈哈哈

const date = new Date('2021-07-23')
//以下三种,前两种会精确到毫秒,后一种只能精确到秒,毫秒用000代替
// 获取到的时间戳要除1000,可获得Unix时间戳,传给后台
const time1 = date.getTime()

const time2 = date.valueof()

const time3 = Date.parse(date)

================ 补充 ================

注意: IOS下,日期格式需为2021/07/23形式,new Date 才不会为空

const date = new Date('2021/07/23');
// 若日期格式为'2021-10-10'
var datestr = '2021-10-10';
const date = new Date(datestr.replace(/\-/g,'/'));


版权声明:本文为thingir原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。