時間戳以及時間格式的相互轉化

1、時間戳轉換成常規的時間格式 ‘yyyy年MM月dd日 hh:mm:ss’ 或 'yyyy-MM-dd’等

// 如給定的時間戳是:"1559700659"
//第一步:時間戳爲10位需*1000,時間戳爲13位的話不需乘1000
let changesjc = new Date(1559700659* 1000);//轉換成普通日期格式
let fk_time = _this.formatDate(changesjc, 'yyyy年MM月dd日 hh:mm:ss');

//輸出fk_time 爲:2019年06月05日 10:11:25


// [formatDate description]格式化時間 --start---
Vue.prototype.formatDate = function(date, fmt) {
    if (!date) return '';
    if (/(y+)/.test(fmt)) {
      fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
    }
    let o = {
      'M+': date.getMonth() + 1,
      'd+': date.getDate(),
      'h+': date.getHours(),
      'm+': date.getMinutes(),
      's+': date.getSeconds()
    };
    for (let k in o) {
      if (new RegExp(`(${k})`).test(fmt)) {
        let str = o[k] + '';
        fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? str : padLeftZero(str));
      }
    }
    return fmt;
};

2、普通日期轉換成時間戳getTime();
sjcStart = Wed Jun 12 2019 00:00:00 GMT+0800 (中國標準時間);

now_sjc = sjcStart.getTime();
//輸入 now_sjc 爲 1560268800000

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章