首页 > 其他分享 >简单JS 日期转时间戳/时间戳转日期

简单JS 日期转时间戳/时间戳转日期

时间:2022-12-15 17:02:35浏览次数:40  
标签:const myStamp JS 日期 时间 Date getMonth getDate

这里我的后端需要秒级  毫秒级不除1000即可

// 日期转时间戳
    toStamp(date) {
        const myDate = new Date(date)
        const stmapEg = Date.parse(myDate) / 1000
        return stmapEg
    },
// 时间戳转日期
    toDate(stamp) {
        const myStamp = new Date(stamp * 1000)
        const YY = myStamp.getFullYear()
        const MM = myStamp.getMonth() + 1 < 10 ? '0' + (myStamp.getMonth() + 1) : (myStamp.getMonth() + 1)
        const DD = myStamp.getDate() < 10 ? '0' + myStamp.getDate() : myStamp.getDate()
        const dateEg = YY + '-' + MM + '-' + DD
        return dateEg
    }

 

标签:const,myStamp,JS,日期,时间,Date,getMonth,getDate
From: https://www.cnblogs.com/shirunfeng/p/16985464.html

相关文章