如何生成这种时间格式?“2018-06-05T19:55:29.000+0800”

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

阅读 4.2k
3 个回答

时间的插件
Moment.js

其中有好多方法,可以转换好多不同时间格式。

moment:

moment().format('YYYY-MM-DDTHH:mm:ss.SSSZZ')

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());
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题