Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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()?

like image 629
Ramesh Rajendran Avatar asked Apr 20 '15 12:04

Ramesh Rajendran


People also ask

Is MomentJS deprecated?

moment().zone is deprecated, use moment().utcOffset instead.

What is moment UTC?

moment. utc can take params as number, string, moment, date etc. This method can be used when the date displayed has to be in UTC format.

How can we get today's date from moment?

Date format conversion with Moment is simple, as shown in the following example. moment(). format('YYYY-MM-DD'); Calling moment() gives us the current date and time, while format() converts it to the specified format.

How do you add one day in a moment?

To add time, pass the key of what time you want to add, and the amount you want to add. moment(). add(7, 'days');


1 Answers

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. ;)

like image 199
sebastienbarbier Avatar answered Oct 01 '22 01:10

sebastienbarbier