Open In App

JavaScript Get the start and end of the day in UTC

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

Given a date, and the task is to determine the start and end of the day of the date using JavaScript. We’re going to discuss a few methods. These are:

JavaScript setHours() Method: This method sets the hour of a date object. This method can be used to set the minutes, seconds, and milliseconds.

 Syntax:

Date.setHours(hour, min, sec, millisec)

Parameters:

  • hour: This parameter is required. It specifies the integer denoting the hours. Values accepted are 0-23, but other values are also allowed.
    -1 means the last hour of the previous day and 24 means the first hour of the next day.
  • min: This parameter is optional. It specifies the integer representing the minutes. Values accepted are 0-59, but other values are also allowed.
    60 means the first minute of next hour and -1 means the last minute of the previous hour.
  • sec: This parameter is optional. It specifies the integer representing the seconds. Values accepted are 0-59, but other values are also allowed.
    60 means the in the first second of next minute and -1 means the last second of previous minute.
  • millisec: This parameter is optional. It specifies the integer representing the milliseconds. Values accepted are 0-999, but other values are also allowed.
    • -1 means the last millisecond of previous second and 1000 means the first millisecond of next second.

JavaScript toUTCString() Method: This method converts a Date object to a string, depending on universal time.

 Syntax: 

Date.toUTCString()

Return Value: It returns a string, representing the UTC date and time as a string.

Example 1: This example gets the first millisecond of the day and last millisecond of the day as well using setHours() method and converting it to UTC format using stoUTCString() method.

Javascript




let startOfDay = new Date();
startOfDay.setHours(0, 0, 0, 0);
 
let endofDay = new Date();
endofDay.setHours(23, 59, 59, 999);
 
console.log(startOfDay.toUTCString());
console.log(endofDay.toUTCString());


Output

Tue, 13 Jun 2023 18:30:00 GMT gfg.html:8:9
Wed, 14 Jun 2023 18:29:59 GMT

Example 2: This example gets the first millisecond of the day and last millisecond of the day but by a different approach than previous one using setHours() method and converting it to UTC format using stoUTCString() method.

Javascript




let startOfDay = new Date();
startOfDay.setHours(0, 0, 0, 0);
 
let endofDay = new Date();
endofDay.setHours(24, 0, 0, -1);
 
console.log(startOfDay.toUTCString());
console.log(endofDay.toUTCString());


Output

Tue, 13 Jun 2023 18:30:00 GMT
Wed, 14 Jun 2023 18:29:59 GMT

We have a complete list of JavaScript Date Objects, to check those please go through this JavaScript Date Object Complete reference article.



Last Updated : 20 Jun, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads