Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
115
rated 0 times [  117] [ 2]  / answers: 1 / hits: 32884  / 9 Years ago, mon, april 20, 2015, 12:00:00

I have reading moment.js document, there have a moment.toISOString() function for helps to formats a string to the ISO8601 standard.



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




moment.toISOString() function using for performance reasons.




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






So my question is.




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


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



More From » momentjs

 Answers
7

You can give a look directly in momentJS source code for such issue :).
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 use .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.




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




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


[#66995] Saturday, April 18, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
mckinleykeyshawnb

Total Points: 281
Total Questions: 99
Total Answers: 111

Location: Saudi Arabia
Member since Sat, Aug 20, 2022
2 Years ago
mckinleykeyshawnb questions
;