-4

i want to print this date: 21.10.2022

with following timestamp: 1666344563

my code:

new Date(parseInt(element.createdAt)).toLocaleDateString())

FYI element.createdAt = '1666344563'

it prints in chrome: 20.01.1970

can someone explain whats happening here?

1 Answers1

1

JavaScript uses miliseconds timestamps. Your value is made from seconds. To make it work just multiply this value by 1000 like so:

new Date(parseInt(element.createdAt) * 1000).toLocaleDateString())

@EDIT: Thanks @NineBerry for noticing. Multiply after parsing.

kuba
  • 141
  • 5