Skip to content

fromUnixTime vs parse vs toDate #2206

Answered by fturmel
JeffBeltran asked this question in Q&A
Discussion options

You must be logged in to vote

Hi @JeffBeltran,

Going through older discussions and I saw that no one answered you. It's probably a bit late, but maybe it'll help someone else. The difference here is simply that fromUnixTime expects a Unix timestamp in seconds (not milliseconds).

You can see it in the example section of the docs, but it's not very obvious. This could be improved, I'll work on a PR. https://date-fns.org/docs/fromUnixTime

So going back to your example, these would be roughly equivalent except the fromUnixTime date will have its milliseconds component lost in the conversion:

const ms = 1613611083646;

fromUnixTime(ms / 1000);
toDate(ms);
parse(ms, "T", new Date());

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@JeffBeltran
Comment options

Answer selected by JeffBeltran
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants