Get the Start and End of the Day using JavaScript

avatar

Borislav Hadzhiev

Fri Jan 14 20222 min read

Get the Start and End of the Day using JavaScript #

Use the setUTCHours() method to get the start and end of the day, e.g. startOfDay.setUTCHours(0, 0, 0, 0). The method takes the hours, minutes, seconds and milliseconds as parameters and sets them for the specified date according to universal time.

index.js
// โœ… In UTC (international time standard) // - keeps code consistent across time zones const startOfDay = new Date(); startOfDay.setUTCHours(0, 0, 0, 0); console.log(startOfDay); const endOfDay = new Date(); endOfDay.setUTCHours(23, 59, 59, 999); console.log(endOfDay); // โœ… In Local Time (time zone the visitor's computer is in) // - will differ for users with different GEOs const start = new Date(); start.setHours(0, 0, 0, 0); console.log(start); const end = new Date(); end.setHours(23, 59, 59, 999); console.log(end);

We used the Date() constructor to get a date object that represents the current date and time.

In the first example, we used the setUTCHours method.

The method takes 4 parameters:

  1. hoursValue - an integer between 0 and 23 that represents the hour.
  2. minutesValue (optional) - an integer between 0 and 59 that represents the minutes.
  3. secondsValue (optional) - an integer between 0 and 59 that represents the seconds.
  4. msValue (optional) - a number between 0 and 999 that represents the milliseconds.
The setUTCHours method sets the provided values on the specified date according to universal time (UTC).

UTC (Coordinated Universal Time) and GMT share the same current time.

The difference between the two is that UTC is a time standard and GMT is a time zone.

UTC is an international time standard, which is used for consistency between time zones.

For consistency, it's best to do store dates and time and do most of your business logic in UTC. You can then format and render the date and time depending on the user's locale.

In the second example, we used the user's local time to get the start and end of the day. Local time refers to the time zone the visitor's computer is in.

index.js
// โœ… In Local Time (time zone the visitor's computer is in) // - will differ for users with different GEOs const start = new Date(); start.setHours(0, 0, 0, 0); console.log(start); const end = new Date(); end.setHours(23, 59, 59, 999); console.log(end);

We used the setHours method instead of setUTCHours.

The difference between the two is that setHours sets the provided values on the date according to local time (the visitor's local time).

Depending on where the user visits your site from, this would yield different results.

The setUTCHours method does the same, but according to universal time.

For example, if you store a local time of midnight (00:00) in your database, you wouldn't know if that's midnight in Tokyo (Japan), in Paris (France), in New York (US), etc. These are all different moments that are hours apart.

For consistency, you should mostly use local time when you have to render a date and time to the user, but store the actual values in UTC.

Join my newsletter

I'll send you 1 email a week with links to all of the articles I've written that week

Buy Me A Coffee