Walter Walter - 10 months ago 72
Javascript Question

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

I am currently using Moment js to parse an ISO 8601 string into date and time, but it is not working properly. What am I doing wrong? And I would take any other easier solutions as well.

The ISO 8601 I would like to parse:

into date in string:
and time in string:

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

Answer Source

With 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');
<script src=""></script>

Or simply with 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];