Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular Date filter is not workig in firefox

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?

like image 768
dip Avatar asked Dec 26 '22 07:12

dip


2 Answers

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.

like image 64
Randy Chung Avatar answered Jan 12 '23 03:01

Randy Chung


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.

like image 43
fracz Avatar answered Jan 12 '23 04:01

fracz