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

js关于时间的各种转换

作者: 头发飘逸 | 来源:发表于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关于时间的各种转换

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

  • js 各种时间转换总结

    tips:接下去会在github写博客,简书不再更新和修改文章,欢迎大家逛逛我的新博客点击查看 ,我会尽量用更容易...

  • js各种时间转换集合

    一、2020-09-03 11:16:02 转化为秒 1. 得到:Wed Nov 04 2020 10:24:00...

  • JS 时间转换

    JS中没有格式化字符串的功能,因此在此整理一下,免得总需要网上查询:我们可以采用给Date添加prototype的...

  • js - 时间转换

    1. 本地时间(日期或者毫秒值)转换成UTC时间 参数说明:date: string | number;参数示例:...

  • js的时间转换:

    将时间戳转换成Date对象(10位的话直接加三个零) 在调试模式下直接console.log(newDate);即...

  • 时间戳的各种转换

    2018-05-30转成时间戳 把当前时间转化成 XXXX-XX-XX

  • js类型转换的各种玩法

    前言 对于object和number、string、boolean之间的转换关系 [ ] Object 与Prim...

  • js对日期的转换

    ## 前言 不清楚js对日期时间怎么转换的,可以参考以下代码 。 ### js日期格式转换 * 字符串变时间 ``...

  • 《Vue.js实战》学习笔记 -时间转换指令

    Vue.js实战 时间转换指令

网友评论

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

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