0

I'm having trouble showing the date in my application. In chrome it works fine, shows the date as expected but in firefox and expolorer it shows NaN-NaN-NaN. I know that if I use a date format with "/" and not "-" than it would work good but I want it to show in a format with "-". Is there a way I can make it work somehow or it only works with "/"?

Here is my code:

formatDate(date) {
    let d = new Date(date),
      month = '' + (d.getMonth() + 1),
      day = '' + d.getDate(),
      year = d.getFullYear();

    if (month.length < 2)
      month = '0' + month;
    if (day.length < 2)
      day = '0' + day;
    return [year, month, day].join('-');
  }
Kerk
  • 283
  • 1
  • 4
  • 24
  • If you have an actual `Date` object, just use the [`DatePipe`](https://angular.io/api/common/DatePipe) in your view. – Heretic Monkey Jun 24 '20 at 13:40
  • Does this answer your question? [Format date as dd/MM/yyyy using pipes](https://stackoverflow.com/questions/35754586/format-date-as-dd-mm-yyyy-using-pipes) – Heretic Monkey Jun 24 '20 at 13:41
  • @HereticMonkey I get an error and the whole page crash in Firefox ```"InvalidPipeArgument: 'Unable to convert "NaN-NaN-NaN" into a date' for pipe 'DatePipe'"``` – Kerk Jun 24 '20 at 14:29
  • Please read the answers to that question again. For instance, the top-voted answer uses the code `{{valueDate | date: 'dd/MM/yyyy'}}` in the view. `valueDate` would be whatever you're now passing into `formatDate`. – Heretic Monkey Jun 24 '20 at 14:32
  • Yes I understand it and can change it in chrome but in firefox its not working – Kerk Jun 24 '20 at 14:50
  • 1
    I think that you're passing the result of `formatDate` to the pipe, rather than an actual `Date` object, since it's complaining about getting `NaN-NaN-NaN`, which is the same thing that you were originally complaining `formatDate` returned... – Heretic Monkey Jun 24 '20 at 14:53
  • Yeah sorry my bad, this cause the problem now it works fine – Kerk Jul 01 '20 at 08:14

0 Answers0