1

After upgrading to Angular 11 the date pipe does not any longer display properly. If I try

 date to display {{1610038272|date:"medium"}} === Jan 07, 2021, 16:51:12

but instead it displays

 Jan 19, 1970, 16:13:58

Is there any breaking change which prevents a proper conversion? Or do I need to set something additonally?

LeO
  • 4,238
  • 4
  • 48
  • 88
  • The `valueOf()` Jan 07, 2021, 16:51:12 is 1595350272321. 1610038272 is way in the past and falls back to 1970 – Benny Halperin Jan 08 '21 at 08:56
  • most likely depending on the time zone. But anyway nothing like 1970!!! – LeO Jan 08 '21 at 08:58
  • What does your console say when you `console.log(new Date(1610038272));`? – Benny Halperin Jan 08 '21 at 09:01
  • strange ... it says `1970-01-19T15:13:58.272Z` so obviously the JS is to be blamed. What does it say on your console? `new Date(1610038272)` – LeO Jan 08 '21 at 09:05
  • Same thing (just different time zone). That's why I asked you to check your console. It has nothing to do with Angular. Why are you surprised if this is the date corresponding to the numeric value? – Benny Halperin Jan 08 '21 at 09:07
  • Isn't this a duplicate of https://stackoverflow.com/questions/38569832/convert-timestamp-to-date-using-angular-2-pipes? – Dorin Baba Jan 08 '21 at 09:11
  • Seems that starting with version 2 of Angular, you have to provide the date as milliseconds since UTC epoch, 1610038272 is the value in seconds. – Dorin Baba Jan 08 '21 at 09:14

1 Answers1

4

You will need to provide the the date in milliseconds

Convert timestamp to date using Angular 2 pipes

{{ 1610038272 * 1000 | date: 'medium' }}

  • 1
    Thanx for the hint. Somehow the Spring backend conversion now serializes the `Date` incorrectly. Don't know why but I take now the `toEpochMilli` and the conversions succeeds. Thx for the direction! – LeO Jan 08 '21 at 09:23
  • Yep, same thing happened with me. We had recently migrated from Date to LocalDateTime, and we had to write a custom jackson serializer for Date to maintain backwards compatibility. Changing getEpochSeconds to toEpochMilli worked! – SpaceBaar Feb 14 '22 at 15:16