moment.toString() 和 moment.toISOString() 之间的区别

Difference between moment.toString() and moment.toISOString()

我阅读了 moment.js 文档,其中有一个 moment.toISOString() 函数可以帮助将字符串格式化为 ISO8601 标准。

Also there have a another one reason for why we use moment.toISOString()

moment.toISOString() function using for performance reasons.

我不知道 toISOString() 性能比 moment.toString() 好。但只有使用 moment.toString()moment.toISOString() 时的结果不同。


所以我的问题是。

你可以直接在momentJS源代码中查看此类问题:)。 Here it is.

export function toString () {
    return this.clone().locale('en').format('ddd MMM DD YYYY HH:mm:ss [GMT]ZZ');
}

export function toISOString () {
    var m = this.clone().utc();
    if (0 < m.year() && m.year() <= 9999) {
        if ('function' === typeof Date.prototype.toISOString) {
            // native implementation is ~50x faster, use it when we can
            return this.toDate().toISOString();
        } else {
            return formatMoment(m, 'YYYY-MM-DD[T]HH:mm:ss.SSS[Z]');
        }
    } else {
        return formatMoment(m, 'YYYYYY-MM-DD[T]HH:mm:ss.SSS[Z]');
    }
}
  • toString 使用 .locale('en').format('ddd MMM DD YYYY HH:mm:ss [GMT]ZZ') momentJS 源代码,在 Javascript
  • 中执行
  • toISOString() 使用由浏览器编译和管理的 javascript 日期对象 (this.toDate().toISOString();)。

Native implementation is ~50x faster, use it when we can

但是,我认为这种差异与大多数项目无关,但现在你知道了。 ;)