Subscribe

Vanilla JavaScript get timestamp

โœ๏ธ

Yes, I always have to google this!

29 May, 2020 ยท 1 min read

Today, I needed 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

Date.now();

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(Date.now() / 1000);

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

See the Pen Vanilla JavaScript get Timestamp by Chris Bongers (@rebelchris) on 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

Spread the knowledge with fellow developers on Twitter
Tweet this tip
Powered by Webmentions - Learn more

Read next ๐Ÿ“–

TypeORM viewEntity

30 Mar, 2022 ยท 5 min read

TypeORM viewEntity

The Record Utility Type in TypeScript

12 Mar, 2022 ยท 3 min read

The Record Utility Type in TypeScript

Join 1372 devs and subscribe to my newsletter