wildlyinaccurate / angular-relative-date

AngularJS filter to provide relative, human-readable dates.
69 stars 23 forks source link

Relative day displayed as "Over a year from now" in Safari #23

Closed trisle closed 8 years ago

trisle commented 8 years ago

It works perfectly fine on Chrome but not on Safari

{{feed.created_at | relativeDate}}

Chrome: 5 hours ago Safari: Over a year from now

Might be related to this SO question?

wildlyinaccurate commented 8 years ago

Could you please provide an example value for feed.created_at?

trisle commented 8 years ago

It outputs a rather normal date string 2016-07-27 07:36:01

I assume it's correct as both Chrome and Firefox shows the relative date just fine.

wildlyinaccurate commented 8 years ago

The relativeDate filter passes strings straight to the new Date() constructor, which is notoriously inconsistent across browsers. There are two ways to fix this:

  1. Use ISO 8601 strings e.g. 2016-07-27T07:36:01Z
  2. Pass a Date object to relativeDate
trisle commented 8 years ago

Thanks for the reply. I actually did try passing in a Date object but wouldn't help.

View {{__date_obj(feed.created_at) | relativeDate}}

Controller

$scope.__date_obj = function(date_str) {
    return new Date(date_str);
}

Still Chrome/Firefox works but not Safari :)

wildlyinaccurate commented 8 years ago

That has the exact same issue in that the new Date(string) constructor is inconsistent across browsers. If you are going to construct a Date object, you either need to use an ISO 8601 string or use the new Date(year, month, day, hour, minutes, seconds) form.