Categories
JavaScript Answers

How to get the UNIX timestamp for the start of today in JavaScript?

Spread the love

Sometimes, we want to get the UNIX timestamp for the start of today in JavaScript.

In this article, we’ll look at how to get the UNIX timestamp for the start of today in JavaScript.

How to get the UNIX timestamp for the start of today in JavaScript?

To get the UNIX timestamp for the start of today in JavaScript, we can use the Date constructor.

For instance, we write:

const now = new Date();
const startOfDay = new Date(now.getFullYear(), now.getMonth(), now.getDate());
const timestamp = startOfDay / 1000;
console.log(timestamp)

to create a new Date instance with the current date and time.

Then we create another Date instance with the year, month and day of the month of today and assigned it to startOfDay.

This will set the time to midnight of the same day.

Then we divide startOfDay by 1000 to return the UNIX timestamp.

Conclusion

To get the UNIX timestamp for the start of today in JavaScript, we can use the Date constructor.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *