DEV Community

Chris Bongers
Chris Bongers

Posted on • Originally published at


Vanilla JavaScript get current timestamp

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;
Enter fullscreen mode Exit fullscreen mode

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();
Enter fullscreen mode Exit fullscreen mode

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

Math.floor( / 1000);
Enter fullscreen mode Exit fullscreen mode

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

Top comments (2)

abdfnx profile image

finally, I found a good post about get current timestamp in Vanilla js πŸ‘

dailydevtips1 profile image
Chris Bongers

You're welcome, be aware they might not last forever 🀟

11 Tips That Make You a Better Typescript Programmer


1 Think in {Set}

Type is an everyday concept to programmers, but it’s surprisingly difficult to define it succinctly. I find it helpful to use Set as a conceptual model instead.

#2 Understand declared type and narrowed type

One extremely powerful typescript feature is automatic type narrowing based on control flow. This means a variable has two types associated with it at any specific point of code location: a declaration type and a narrowed type.

#3 Use discriminated union instead of optional fields


Read the whole post now!