Search code examples
javascriptangulartypescriptdateprimeng

new Date() works in Chrome but throws 'Invalid date' in Firefox


Used ng prime datepicker in application.

Getting date in Fri May 14 2021 00:00:00 GMT+0530 (India Standard Time) format as response from api.

Then converting Fri May 14 2021 00:00:00 GMT+0530 (India Standard Time) this response with new Date().

Then binding the response to ng prime datepiker.

Working fine in chrome. In firefox it is showing "Invalid date".


Solution

  • See the documentation for the Date constructor:

    A string value representing a date, specified in a format recognized by the Date.parse() method. (These formats are IETF-compliant RFC 2822 timestamps, and also strings in a version of ISO8601.)

    Since you aren't passing one of those formats, you are dependent on implementations having non-standard support for your format.

    Chrome does. Firefox doesn't.

    Use a date parsing library that lets you specify the format (such as date-fns/parse) or change the API so it outputs dates in the standard format.