时间戳与标准时间的相互转换

参考链接:https://www.cnblogs.com/jf-67/p/8007008.html

1、时间戳 ->中国标准时间

    let tamp = 1542097329747;  // 当参数为数字的时候,那么这个参数就是时间戳,被视为毫秒,创建一个距离1970年1月一日指定毫秒的时间日期对象。
    let tamp1 = "2018-11-13 12:50:30";
    let tamp2 = "2018-11-13";
    let time = new Date(tamp)
    let time1 = new Date(tamp1)
    let time2 = new Date(tamp2)
    console.log(time)  //Tue Nov 13 2018 16:22:09 GMT+0800 (中国标准时间)
    console.log(time1) //Tue Nov 13 2018 12:50:30 GMT+0800 (中国标准时间)
    console.log(time2) //Tue Nov 13 2018 08:00:00 GMT+0800 (中国标准时间)

2、中国标准时间 ->时间戳

    //已有一个中国标准时间,现在可以获取其时间戳
    let today = new Date()
    //获取其时间戳(单位:毫秒,通过/1000转换为秒)
    let timestap = today.getTime()
    console.log(timestap) //1542097849679
    //获取指定日期当天0点0分0秒的时间戳(单位:秒)
    let time1 = today.setHours(0,0,0,0)/1000
    console.log(time1) //1542038400
    //获取指定日期当天0点0分0秒的时间戳(单位:秒)
    let time2 = today.setHours(23,59,59,0)/1000
    console.log(time2) //1542124799
    //获取指定几点几分几秒的时间戳在查询列表的时候有很大作用
 
原文地址:https://www.cnblogs.com/zjingjing/p/9952804.html