Issue
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?
Solution
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.
Answered By - kuba
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.