Vanilla JavaScript get current timestamp

Vanilla JavaScript get current timestamp

Chris Bongers
·Jan 18, 2021·

2 min read

Listen to this article

Today I needed to get a timestamp to include in a unique file that I'm generating with Node.js.

It's one of these things that after 17 years, I still google to this day... Call me stupid or just ignorant in not saving it in my brain.

Getting a Timestamp in JavaScript;

Wow, That was the one thing I can't get in my head?! Yes. There are other ways of doing it, so here are some alternatives that do the same:

new Date().getTime();
new Date().valueOf();

All three methods will get a millisecond timestamp; we can convert it to seconds by doing so:

Math.floor( / 1000);

Feel free to have a look and play on this Codepen.

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

Did you find this article valuable?

Support Chris Bongers by becoming a sponsor. Any amount is appreciated!

See recent sponsors Learn more about Hashnode Sponsors
Share this