js封裝調用 時間戳轉時間

本人php小白一枚  在volist 或者 foreach渲染模板須要把時間戳轉時間的時候  直接就{$v.addtime | date="Y-m-d H:i:s"}php

今天在用ajax渲染模板的時候發現不會用js把時間戳轉時間  就百度了一個方法  分享出來你們看吧;ajax

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

直接在須要轉時間的地方調用就行了  是否是很簡單 this

'+ timest(this.addtime) +'
相關文章
相關標籤/搜索