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

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

慕虎7371278
浏览 1961回答 3
3回答

九州编程

时间的插件Moment.js其中有好多方法,可以转换好多不同时间格式。

墨色风雨

moment没有直接的方案,我自己贴个方案好了。Date.prototype.toIsoString = function() {&nbsp; &nbsp; var tzo = -this.getTimezoneOffset(),&nbsp; &nbsp; &nbsp; &nbsp; dif = tzo >= 0 ? '+' : '-',&nbsp; &nbsp; &nbsp; &nbsp; pad = function(num) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var norm = Math.floor(Math.abs(num));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return (norm < 10 ? '0' : '') + norm;&nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; padMilli = function(num) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var norm = Math.floor(Math.abs(num));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (norm >= 10 && norm < 100) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return '0' + norm;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (norm < 10) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return '00' + norm;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return norm;&nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; return this.getFullYear() +&nbsp; &nbsp; &nbsp; &nbsp; '-' + pad(this.getMonth() + 1) +&nbsp; &nbsp; &nbsp; &nbsp; '-' + pad(this.getDate()) +&nbsp; &nbsp; &nbsp; &nbsp; 'T' + pad(this.getHours()) +&nbsp; &nbsp; &nbsp; &nbsp; ':' + pad(this.getMinutes()) +&nbsp; &nbsp; &nbsp; &nbsp; ':' + pad(this.getSeconds()) +&nbsp; &nbsp; &nbsp; &nbsp; '.' + padMilli(this.getMilliseconds()) +&nbsp; &nbsp; &nbsp; &nbsp; dif + pad(tzo / 60) + pad(tzo % 60);}var dt = new Date();console.log(dt.toIsoString());
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript