在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/HTML/ js/jquery如何解析時(shí)間戳為指定格式

js/jquery如何解析時(shí)間戳為指定格式

clipboard.png

不會(huì)解析時(shí)間,百度了很多都方法都是報(bào)錯(cuò)的

clipboard.png

我想轉(zhuǎn)成yyyy-MM-dd HH:mm:ss格式的

回答
編輯回答
情殺

用庫(kù),date-fns 或者 moment

或者,這里有一段老代碼,直接擴(kuò)展 Date 對(duì)象的

Date.prototype.format = function(format = "yyyy-MM-dd") {
    var o = {
        //month 
        "M+": this.getMonth() + 1,
        // day  + 1
        "d+": this.getDate(),
        // hour (24小時(shí))
        "H+": this.getHours(),
        // 小時(shí)(12小時(shí))
        "h+": this.getHours() % 12,
        // minute 
        "m+": this.getMinutes(),
        // second 
        "s+": this.getSeconds(),
        // quarter 
        "q+": Math.floor((this.getMonth() + 3) / 3),
        // millisecond 
        "S": this.getMilliseconds()
    };

    if (/(y+)/i.test(format)) {
        format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    }

    for (var k in o) {
        if (new RegExp("(" + k + ")").test(format)) {
            format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
        }
    }
    return format;
};
2017年2月5日 10:18