问题
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 NaN
s 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