How to handle ISO 8601 in date and time format using Moment js in Javascript?

I am currently using Moment js to parse an ISO 8601 string at a date and time, but it does not work correctly. What am I doing wrong? And I would make any other simpler decisions.

ISO 8601 I would like to parse: "2011-04-11T10:20:30Z" into a line in a line: "2011-04-11" and the time in a line: "10:20:30"

And I tried console.log(moment("2011-04-11T10:20:30Z" ,moment.ISO_8601)) and console.log(moment("2011-04-11T10:20:30Z" , ["YYYY",moment.ISO_8601]) as a test, but it just returns an object with all the various properties.

+5
source share
2 answers

From moment.js

 var str = '2011-04-11T10:20:30Z'; var date = moment(str); var dateComponent = date.utc().format('YYYY-MM-DD'); var timeComponent = date.utc().format('HH:mm:ss'); console.log(dateComponent); console.log(timeComponent); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.15.1/moment.min.js"></script> 

Or just string manipulation

 var str = '2011-04-11T10:20:30Z'; var parts = str.slice(0, -1).split('T'); var dateComponent = parts[0]; var timeComponent = parts[1]; console.log(dateComponent); console.log(timeComponent); 
+10
source

There are two parts at the time of the operation: reading the date / time and spitting out. You have the first part:

 moment("2011-04-11T10:20:30Z") 

but then you need to call the output function, for example:

 moment("2011-04-11T10:20:30Z").format('YYYY-MM-DD h:mm:ss a') 
+9
source

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


All Articles