Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

unix timestamp to age in javascript

If you have an old UNIX timestamp in JavaScript... is there a way I can get the number of the age of that timestamp?

Like, the age of how old that unix timestamp is from when it is... to now.

Can you do that in JavaScript?

like image 975
test Avatar asked Jan 30 '26 18:01

test


1 Answers

Assuming your timestamp is in milliseconds, you can get the the age in milliseconds using:

(new Date()).getTime() - timestamp;

If your timestamp is in seconds, multiply it by 1000 before subtracting.

You can then divide by the appropriate factor to convert to seconds/minutes/hours etc. For example, to get the age in years, you would use:

((new Date()).getTime() - timestamp) / (1000 * 60 * 60 * 24 * 365);