美文网首页前端
js关于时间的各种转换

js关于时间的各种转换

作者: w晚风 | 来源:发表于2020-09-23 22:58 被阅读0次

    前言:

    工作中总是会遇到时间转换的情况,好多时候忘记了都得上吧百度查找下问题,所以在这里准备把常用的方法记录下来以便后续工作的更加方便

    注意:

    1. 此处的标准时间格式为 2018-03-23 13:35:47

    2. 此处的毫秒为 js中getTime();

    3. 时间戳是指格林威治时间1970年01月01日00时00分00秒(北京时间1970年01月01日08时00分00秒)起至现在的总 秒 数

    4. getTime() 返回 1970 年 1 月 1 日至今的总 毫秒 数,不是总秒数

    一、将毫秒转换成标准时间格式

    /**
     * @param {数值类型} date
     */
    function formatDate(date){
        date = new Date(date);
        var y  =  date.getFullYear();
        var m  =  date.getMonth()+1;
        var d  =  date.getDate();
        var h  =  date.getHours();
        var m1  =  date.getMinutes();
        var s  =  date.getSeconds();
        m  =  m<10?("0"+m):m;
        d  =  d<10?("0"+d):d;
        return y+"-"+m+"-"+d+" "+h+":"+m1+":"+s;
    }
    //  例
    console.log(formatDate(1521783347000)); //  2018-03-23 13:35:47  参数为Number
    

    二、将标准时间格式转换成毫秒

    var date = Date.parse(new Date('2018-03-23 13:35:47'));// 参数为String
    console.log(date);  //  1521783347000  单位:毫秒
    

    三、格式化Thu May 12 2017 08:00:00 GMT+0800 (中国标准时间) 转字符串

    function checkTime(i){
          if (i < 10){
               i =  "0" + i
           }
          return i;
    }
    var date = new Date('Thu May 12 2020 08:00:00 GMT+0800 (中国标准时间)');  
    let date_value = date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate() + ' ' + checkTime(date.getHours()) + ':' + checkTime(date.getMinutes()) + ':' + checkTime(date.getSeconds());  
    console.log(date_value) //  最后显示yyyy-MM-dd hh:mm:ss  2020-5-12 08:00:00
    

    四、字符串Wed Dec 13 2017 16:00:00 GMT+0800 (中国标准时间)

    //  如何将2017-12-13 16:00:00转换成标准时间?
    function str_time(date) {  
        var t = Date.parse(date);  
        if (!isNaN(t)) {  
           return new Date(Date.parse(date.replace(/-/g, "/")));  
        }
        else{  
           return new Date();  
        }  
    }
    console.log(str_time('2017-12-13 16:00:0')) // 输入为:Wed Dec 13 2017 16:00:00 GMT+0800 (中国标准时间)
    

    五、获取当前时间日期 2020-05-29 17:28:28

    function getDate(){
        const myDate = new Date();
        const getFullYear = myDate.getFullYear();
        const getMonth = myDate.getMonth()+1 > 9? myDate.getMonth()+1:'0'+(myDate.getMonth()+1);
        const date = myDate.getDate();
        const getHours = myDate.getHours();  
        const getMinutes = myDate.getMinutes();
        const getSeconds = myDate.getSeconds(); 
        
        const t = getFullYear+'-'+getMonth+'-'+date+' '+getHours+':'+getMinutes+':'+getSeconds;  
        return t;
    }
    console.log(getDate()); // 输出:2020-09-23 22:23:37
    

    六、js获取当前时间戳

    //  第一种方法: Date.parse(new Date()) 获取的时间戳是把毫秒改成000显示,例:1600871731000
    var time1 = Date.parse(new Date());
    console.log(time1) 
    
    // 第二种方法: new Date().valueOf() 是获取了当前毫秒的时间戳,例:1600871731734
    var time2 = new Date().valueOf();
    console.log(time2) 
    
    // 第三种方法: new Date().getTime() 是获取了当前毫秒的时间戳 例:1600871731735
    var time3 = new Date().getTime();
    
    

    相关文章

      网友评论

        本文标题:js关于时间的各种转换

        本文链接:https://www.haomeiwen.com/subject/lahayktx.html