JavaScript--日期格式化


平時用前端框架做項目,從數據庫中讀取的時間,老是是時間戳或者其它的一些格式,總結一下兩個常用的格式化日期的方式!

//js將"2018-05-19T08:04:52.000+0000"這種格式的時間轉化為正常格式
function DateTimeFormatter(value) {
    if (value != null) {
        var dateee = new Date(value).toJSON();
        var date = new Date(+new Date(dateee) + 8 * 3600 * 1000).toISOString().replace(/T/g, ' ').replace(/\.[\d]{3}Z/, '');
        return date;
    }
    else {
        return null;
    }
}
//將時間戳轉換為正常格日期
function DateTimeFormatters(value) {
    if (value == undefined) {
        return "";
    }
    /*json格式時間轉js時間格式*/
    value = value.substr(1, value.length - 2);
    var obj = eval('(' + "{Date: new " + value + "}" + ')');
    var dateValue = obj["Date"];
    if (dateValue.getFullYear() < 1900) {
        return "";
    }
    var date = new Date(dateValue);
    var y = date.getFullYear();
    var m = date.getMonth() + 1;
    var d = date.getDate();
    return y + '/' + m + '/' + d;
    return date;
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM