Rushikesh jogle
Rushikesh jogle

Reputation: 591

new Date().toLocaleString not working on firefox

In json response i am getting date in GMT format like this 2016-03-09T09:55:47.107111 . Whenever i am trying to convert into local date /time format using following code :-

new Date('2016-03-09T09:55:47.107111').toLocaleString()

I am getting output like this :-

Chrome

"09/03/2016, 15:25:47"   Which is correct 

Mozilla Firefox

"3/9/2016, 9:55:47 AM"  Which is wrong 

new Date('2016-03-09T09:55:47.107111').toLocaleString() is not working on Firefox .

Upvotes: 2

Views: 4044

Answers (3)

Dima Vinogradov
Dima Vinogradov

Reputation: 600

You can always specify the localization by passing it to the toLocaleString function. for example:

new Date('2016-03-09T09:55:47.107111').toLocaleString("en-GB");

Will result in:

09/03/2016, 11:55:47

in both Chrome & Firefox.

Be aware:

new Date('2016-03-09T09:55:47.107111').toLocaleString("en-GB");

new Date('2016-03-09 09:55:47.107111').toLocaleString("en-GB");

will give you different time output (first will add the locale difference from UTC, the second won't.

Upvotes: 0

str
str

Reputation: 45029

According to the MDN specification of Date, "dateString" can be either IETF-compliant RFC 2822 timestamps or a version of ISO8601. Your date string is neither of it. I'm not even sure what "107111" in the end of your string should be, so how should a computer figure that out?

In general, it is always advisable to use date strings in the format "YYYY-MM-DDTHH:mm:ss.sssZ".

Upvotes: 3

mitra p
mitra p

Reputation: 440

In js file you write

$scope.date=new Date('2016-03-09T09:55:47.107111');

in html file you write

{{date | date :'dd/MM/yyyy h:mm:ss'}}

it will working any browser

Upvotes: 1

Related Questions