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

女生的网名这么多〃 提交于 2019-12-03 11:16:42

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');
console.log(dateComponent);
console.log(timeComponent);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.15.1/moment.min.js"></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];
console.log(dateComponent);
console.log(timeComponent);

There's two parts to the moment operation: reading the date/time in, and spitting it back out. You've got the first part:

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

but then you need to call an output function, eg:

moment("2011-04-11T10:20:30Z").format('YYYY-MM-DD h:mm:ss a')
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!