Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Date Filter: Safari throws error if the date is an Invalid Date. #13415

Closed

Description

@QuentinFchx

Safari's JSON.stringify doesn't handle invalid dates correctly. It throws a RangeError: Invalid Date.
Whenever you try to interpolate an Invalid Date with the date filter, it breaks.
{{ 1/0 | date }} http://plnkr.co/edit/z8xKljNoR1GdsMQypzlq?p=preview

In https://github.com/angular/angular.js/blob/master/src/ng/interpolate.js
parseStringifyInterceptor > stringify > toJson > JSON.stringify

This issue might be too specific for a fix to be included in Angular though. Thoughts ?
I'd be glad to provide a PR.
Something along :

if(isNaN(date.getTime())){
  return '';
}

here https://github.com/angular/angular.js/blob/master/src/ng/filter/filters.js#L489

Activity

  1. petebacondarwin commented on Dec 7, 2015

    @petebacondarwin
    Contributor

    In my tests the date filter, on Safari, is returning a Date object correctly. It is just that this object represents an 'invalid' date.
    It would seem to me that the problem is in our stringify function, which should catch the error thrown by JSON.stringify for invalid Date objects.

  2. added this to the 1.4.9 milestone on Dec 7, 2015
  3. modified the milestones: 1.4.9, 1.4.10 on Jan 21, 2016
  4. modified the milestones: 1.4.10, 1.5.1 on Jan 28, 2016
  5. 18 remaining items

  6. BobChao87 commented on Mar 19, 2016

    @BobChao87
    Contributor

    I'm having some issues with the actual build.

    Travis seems to have failed on apt-get in build 115755797 for every test. All tests currently pass on my local build and I suspect this to be an issue with the build environment at that time. How should I go about rectifying this since there doesn't seem to be retry option?

    Furthermore, in PR #14221 , @googlebot apparently found my CLA for the pull request, but not for my commits, even though both were performed on the same account. Is there a recommended way to rectify this issue as well? It could possibly be related to the fact that my email is hidden in GitHub. I'd prefer not to change that, but would if it is required.

  7. added a commit that references this issue on Mar 23, 2016
    a859cda
  8. modified the milestones: 1.5.3, 1.5.4 on Mar 25, 2016
  9. modified the milestones: 1.5.x, 1.5.4 on Apr 5, 2016
  10. added a commit that references this issue on May 21, 2016
    e00f9f6
  11. plong0 commented on Aug 8, 2016

    @plong0

    I just updated to 1.4.12 and I still receive the RangeError: Invalid Date in Safari.

    In my case I have an expression like: {{ mything.timestamp * 1000 | date:'medium' }} and the error is happening when mything.timestamp is undefined, so date filter ends up getting a NaN value as input.

    This solution worked for me:

    filters.js::591

    if (isNumber(date)) {
        date = new Date(date);
    }
    

    change to:

    if (isNumber(date) && !isNaN(date)) {
        date = new Date(date);
    }
    

    I was going to submit a PR for this, but then I found this issue was closed already so I just updated.

    Should I go ahead with a PR to add the !isNaN check?

  12. BobChao87 commented on Aug 8, 2016

    @BobChao87
    Contributor

    Hi, @plong0, that fix addresses only a subset of the problems with this issue, though still sounds like a useful feature to have. For more details on why it doesn't address everything, check out #14221. I'm not sure on what the exact procedure is on proceeding from here, I'll let the Angular team handle that. I suspect you'll want to open a new issue, but I cannot be certain.

  13. gkalpak commented on Aug 8, 2016

    @gkalpak
    Member

    I haven't made up my mind if this should be fixed or not. (It will be a breaking change and I am not sure the benefits are enough to justify it).
    But you can sure go ahead a create a PR and kick off the discussion 😄

  14. plong0 commented on Aug 9, 2016

    @plong0

    Hm, I never thought that it could be a breaking change - but now that you mention it, I can imagine how it could be.

    In the mean time, I have a very simple workaround where I pass my date values through a timestamp filter before passing them to date filter. The timestamp filter is doing a simple check so the date filter will get either a valid timestamp integer (* 1000 if conversion is needed) or null if the input wouldn't convert to a valid date object.

  15. added a commit that references this issue on Sep 28, 2024
    eaa1119
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions