Convert system date to ISO format using pulses

I am trying to convert the system date to the ISO format below using momentjs

2015-02-17T19:05:00.000Z 

However, I cannot find the parameter that I need to use in order to get it in the format I want. I tried below code snippet.

 moment().format("YYYY-MM-DD HH:mm Z"); 

I get the output as 2015-02-02 17: 24 + 05: 30.

How can I get it like 2015-02-02T17: 24: 00.000Z

+6
source share
1 answer

This is pretty well described in docs . But, they are long, so here are the features:

For some reason, the wayjs ISO 8601 definition is different from ECMAScript , so it is not built-in. The format is YYYY-MM-DDTHH:mm:ss.sssZ and it must be in UTC (this means Z ).

So, moment().utc() guarantees the correct time zone.

Then format it:

 moment().utc().format("YYYY-MM-DDTHH:mm:ss.SSS[Z]"); // 2015-02-02T21:38:04.092Z 

Z escaped with square brackets . We can do this safely because we forced UTC.

The remaining characters indicate different time elements in accordance with the format table.


You can also do what RobG said and use your own date object. If you start with a minute:

 moment().toDate().toISOString( ) // 2015-02-02T21:40:06.395Z 
+10
source

Source: https://habr.com/ru/post/981866/


All Articles