简体   繁体   English

Angular.js日期解析

[英]Angular.js date parsing

I'm working on a project in which I need to implement a list of dates. 我正在做一个需要实现日期列表的项目。 I have been able to pull the data from the api, however I have been having troubles parsing the format within these dates. 我已经能够从api中提取数据,但是在这些日期内解析格式时遇到了麻烦。

The data that I have been able to pull looks similar to this 我能够提取的数据与此类似

[["2015-10-05T13:00:00Z","2015-10-05T21:00:00Z"],
["2015-10-06T13:00:00Z","2015-10-06T21:00:00Z"],
["2015-10-07T13:00:00Z","2015-10-07T21:00:00Z"]] 

Which is sweet that i've been able to pull.....but as you can see, the dates are not really what a user will need. 我能够拉出这真是太好了.....但是您可以看到,日期并不是用户真正需要的。

I've been looking into moment() methods. 我一直在研究moment()方法。 ( http://momentjs.com/ ) However the problems that i've run into is that the methods that you can use with moment() is that i've only been able to make anything work with one date, not with an array of dates such as what i have. http://momentjs.com/ )但是我遇到的问题是,您可以使用moment()使用的方法是,我只能在一个日期上执行任何操作,而不能使用一个日期进行操作。日期数组,例如我所拥有的。

So my question is, are there any alternatives to moment(), or better ways of parsing an array of dates? 所以我的问题是,是否可以使用moment()替代方法,还是可以更好地解析日期数组?

You'd have to loop and parse.. you can do it fairly simply with a .map call (with momentjs ): 您必须循环和解析..您可以使用.map调用(使用momentjs )相当简单地完成此操作:

var formattedDates = array.map(function(inner) {
    return inner.map(function(d) {
        return moment(d).format("MM/DD/YYYY hh:mm A");
    });
}).reduce(function(p, c) {
    return p.concat(c);
});

Demo: http://jsfiddle.net/6ncpspc0/ 演示: http//jsfiddle.net/6ncpspc0/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM