vue將時(shí)間戳轉(zhuǎn)換成日期格式

(1)創(chuàng)建一個(gè)處理時(shí)間格式的js,內(nèi)容如下:

                Date.prototype.Format = function(fmt) { //
                    let o = {
                        "M+" : this.getMonth()+1,                 //月份
                        "d+" : this.getDate(),                    //日
                        "h+" : this.getHours(),                   //小時(shí)
                        "m+" : this.getMinutes(),                 //分
                        "s+" : this.getSeconds(),                 //秒
                        "q+" : Math.floor((this.getMonth()+3)/3), //季度
                        "S"  : this.getMilliseconds()             //毫秒
                    };
                    if(/(y+)/.test(fmt))
                        fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));
                    for(var k in o)
                        if(new RegExp("("+ k +")").test(fmt))
                            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
                    return fmt;
                };

(2)在vue文件中需要格式化時(shí)間戳的地方,使用filters過(guò)濾器,做如下處理:


<template>
  <div class="date">{{item.pass_time | Format }}</div>
</template>
 
<script type="text/ecmascript-6">
  import {Format } from '../../utils/Format .js''
  export default {
    filters: {
      //注意:time應(yīng)為格式為13位unix時(shí)間戳
      //年-月-日 時(shí):分:秒
      Format (time) {
        time = time * 1000
        let date = new Date(time)
        console.log(new Date(time))
        return Format (date, 'yyyy-MM-dd hh:mm')
      }
      //年-月-日
      Format (time) {
        time = time * 1000
        let date = new Date(time)
        console.log(new Date(time))
        return Format (date, 'yyyy-MM-dd hh:mm')
      }
    }
  }
</script>
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容