javascript moment.toString() 和 moment.toISOString() 的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29747550/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 10:59:59  来源:igfitidea点击:

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

javascriptmomentjs

提问by Ramesh Rajendran

I have reading moment.jsdocument, there have a moment.toISOString()function for helps to formats a string to the ISO8601standard.

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

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

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

moment.toISOString()function using for performance reasons.

moment.toISOString()出于性能原因使用的函数。

I don't know toISOString()the performance best than moment.toString().But only the result was difference while using moment.toString()and moment.toISOString().

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



So my question is.

所以我的问题是。

  • Why we should use moment.toISOString()? for performancereasons?

  • And what is the difference between moment.toISOString()and moment.toString()?

  • 为什么要使用 moment.toISOString()?出于性能原因?

  • 和之间有什么区别moment.toISOString()moment.toString()

回答by sebastienbarbier

You can give a look directly in momentJS source code for such issue :). Here it is.

您可以直接在 momentJS 源代码中查看此类问题:)。 在这里

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]');
    }
}
  • toStringuse .locale('en').format('ddd MMM DD YYYY HH:mm:ss [GMT]ZZ')which is momentJS source code, executed in Javascript
  • toISOString()use javascript Date object (this.toDate().toISOString();) which is compile and managed by your browser.
  • toString使用的.locale('en').format('ddd MMM DD YYYY HH:mm:ss [GMT]ZZ')是 momentJS 源代码,在 Javascript 中执行
  • toISOString()使用this.toDate().toISOString();由浏览器编译和管理的javascript Date 对象 ( )。

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

本机实现速度快约 50 倍,尽可能使用它

However, I think such difference is not relevant for a most projects, but now you know. ;)

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