JavaScript | Get the start and end of the day in UTC

Given a date, the task is to determine the start and end of the day using javascript. We’re going to discuss a few techniques.
First few methods to know.

  • 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.

    Return value:
    It returns a number, representing the number of milliseconds between the date object and midnight January 1, 1970.

  • 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.