As developers, we use timestamps a lot, for storing when last a function was called, but even too safe dates for specific actions.
The Unix epoch (timestamp) started 1st January 1970 at 00:00:00 and ticks every second, so the number you see is the number of seconds since that moment.
In case you are wondering, yes there is a limit to this method and it will become obsolete in 2038. (Since we run out of space to store the seconds in this 32-bit integer).
As for now, it's still a vastly used method, you might have to store or compare to something that was previously set.
This one is my favorite, since it's the most readable one, and it works on every browser.
Another version of this:
If you don't have to support the older browser you can also use:
All of these will return a timestamp like this:
That means we need to divide the number by 1000
Math.floor(new Date().getTime() / 1000);
Now we will get:
You can check these timestamps on epochconverter.com.
Converting a timestamp into a date
Perhaps you are wondering how to convert this timestamp back into a date?
We can simply pass the timestamp to the date object.
We can then convert that in a string for instance:
new Date(1610341839403).toString(); // 'Mon Jan 11 2021 07:10:39 GMT+0200 (South Africa Standard Time)'
Thank you for reading, and let's connect!
Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter