DEV Community

Cover image for Vanilla JavaScript get Timestamp
Chris Bongers
Chris Bongers

Posted on • Originally published at daily-dev-tips.com

1 1

Vanilla JavaScript get 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

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

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

SurveyJS custom survey software

JavaScript UI Library for Surveys and Forms

Generate dynamic JSON-driven forms directly in your JavaScript app (Angular, React, Vue.js, jQuery) with a fully customizable drag-and-drop form builder. Easily integrate with any backend system and retain full ownership over your data, with no user or form submission limits.

View demo

Top comments (0)

SurveyJS custom survey software

JavaScript Form Builder UI Component

Generate dynamic JSON-driven forms directly in your JavaScript app (Angular, React, Vue.js, jQuery) with a fully customizable drag-and-drop form builder. Easily integrate with any backend system and retain full ownership over your data, with no user or form submission limits.

Learn more

👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someone’s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay