鍍金池/ 問答/HTML/ 如何生成這種時間格式?“2018-06-05T19:55:29.000+0800

如何生成這種時間格式?“2018-06-05T19:55:29.000+0800”

這種時間格式是怎么生成的呢?“2018-06-05T19:55:29.000+0800”
目前只知道 (new Date()).toISOString()可以生成"2018-06-22T08:41:09.093Z",但是不知道上面的格式怎么搞

回答
編輯回答
過客
2017年8月30日 01:36
編輯回答
朕略傻

moment沒有直接的方案,我自己貼個方案好了。

Date.prototype.toIsoString = function() {
    var tzo = -this.getTimezoneOffset(),
        dif = tzo >= 0 ? '+' : '-',
        pad = function(num) {
            var norm = Math.floor(Math.abs(num));
            return (norm < 10 ? '0' : '') + norm;
        },
        padMilli = function(num) {
            var norm = Math.floor(Math.abs(num));
            if (norm >= 10 && norm < 100) {
                return '0' + norm;
            }
            if (norm < 10) {
                return '00' + norm;
            }
            return norm;
        };
    return this.getFullYear() +
        '-' + pad(this.getMonth() + 1) +
        '-' + pad(this.getDate()) +
        'T' + pad(this.getHours()) +
        ':' + pad(this.getMinutes()) +
        ':' + pad(this.getSeconds()) +
        '.' + padMilli(this.getMilliseconds()) +
        dif + pad(tzo / 60) + pad(tzo % 60);
}

var dt = new Date();
console.log(dt.toIsoString());
2018年3月19日 18:22
編輯回答
雨萌萌

時間的插件
Moment.js

其中有好多方法,可以轉(zhuǎn)換好多不同時間格式。

2018年4月22日 02:55