Angular Date filter is not workig in firefox

南笙酒味 提交于 2019-12-07 09:42:55

问题


I am using date filter to format my date in my angular application.

In Firefox, I'm getting the date value as

undefined NaN, NaN NaN:NaN:NaN PM

In Chrome its works perfectly as

Jun 25, 2014 7:22:47 AM

My code is as follows.

var formatDate = new Date(info.list[i].date);
var newDate=$filter('date')(formatDate, 'medium');

How do I get it to work in Firefox?


回答1:


I ran into this issue and found that the problem was Chrome/Opera and Firefox/Safari have different tolerances for creating a new Javascript Date object.

This works in Chrome and Opera, but not Firefox and Safari:

var myDate = new Date("2014-08-12 11:46:26.509")

This works in all mentioned browsers:

var myDate = new Date("2014-08-12T11:46:26.509")

Once I had a proper Date object created, the AngularJS date filter works as expected.




回答2:


There is a moment.js library that facilitates dates parsing and that works cross-browser.

I also had a problem with NaNs in firefox and I was using

var myDate = Date.parse(date);

for Date creation. Switched to:

var myDate = moment(date).toDate();

and everything works flawlessly.



来源:https://stackoverflow.com/questions/24545181/angular-date-filter-is-not-workig-in-firefox

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!