fromUnixTime vs parse vs toDate #2206
-
This might be something really dumb that im missing but i would think these 3 methods would do the same thing but it looks like From the docs
what am i missing? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
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 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 const ms = 1613611083646;
fromUnixTime(ms / 1000);
toDate(ms);
parse(ms, "T", new Date()); |
Beta Was this translation helpful? Give feedback.
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: