1. 程式人生 > >vue v-for 獲取了一個時間戳, 然後加過濾器, 變成了NaN-NaN-NaN,為什麼?

vue v-for 獲取了一個時間戳, 然後加過濾器, 變成了NaN-NaN-NaN,為什麼?

vue v-for 獲取了一個時間戳, 然後加過濾器, 變成了NaN-NaN-NaN,不加過濾器,能夠正常顯示出來。這個什麼原因

後臺獲取了一個數據,v-for迴圈顯示出了 ws.craeateTime 是一個13位的毫秒數,formatDate是過濾器

相關程式碼

// 請把程式碼文字貼上到下方(請勿用圖片代替程式碼)

<div  v-for=" ws in wmessage"  class="wmessageBoxTop">
    <div class="wmessageBoxTopleft"><p>警告</p></div>
    <ul class="wmessageBoxTopright">
        <li><p>未處理</p></li>
        <li class="wmessageBoxTopTime">
        <div><p>{{ws.createTime | formatDate}}</p><p>>>></p></div>
        </li>
    </ul>
</div>

選擇器

filters: {
       formatDate: function (value) {
        var date = new Date(value);//時間戳為10位需*1000,時間戳為13位的話不需乘1000
        var Y = date.getFullYear() + '-';
        var M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
        var D = date.getDate() + ' ';
        var h = date.getHours() + ':';
        var m = date.getMinutes() + ':';
        var s = date.getSeconds();
        return Y+M+D+h+m+s;
      }
    },

解決方案 

formatDate: function (value) {
    var val = JSON.parse(value)
    var date = new Date(val);//時間戳為10位需*1000,時間戳為13位的話不需乘1000
    var Y = date.getFullYear() + '-';
    var M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
    var D = date.getDate() + ' ';
    var h = date.getHours() + ':';
    var m = date.getMinutes() + ':';
    var s = date.getSeconds();
    return Y+M+D+h+m+s;
}