JavaScript Date setUTCHours() Method



The JavaScript Date.setUTCHours() method is used to set the hour of a Date object according to Coordinated Universal Time (UTC). In addition to that, we can also set the minutes, seconds, milliseconds (which are optional).

UTC stands for Coordinated Universal Time. It is the primary time standard by which the world regulates clocks and time. Whereas, the India Standard Time (IST) is the time observed in India, and the time difference between IST and UTC is as UTC+5:30 (i.e. 5 hours 30 minutes).

Syntax

Following is the syntax of JavaScript Date setUTCHours() method −

setUTCHours(hoursValue, minutesValue, secondsValue, millisecondsValue);

Parameters

This method accepts four parameters. The same is described below −

  • hoursValue An integer representing the hour (0 to 23).
    • If -1 is provided, it will result in the last hour of the previous day.
    • If 24 is provided, it will result in the first hour of the next day.
  • minutesValue (optional) An integer representing the minutes (0 to 59). If not provided, minutes will be set to 0.
    • If -1 is provided, it will result in the last minute of the previous hour.
    • If 60 is provided, it will result in the first minute of the next hour.
  • secondsValue (optional) An integer representing the seconds (0 to 59). If not provided, seconds will be set to 0.
    • If -1 is provided, it will result in the last second of the previous minute.
    • If 60 is provided, it will result in the first second of the next minute.
  • millisecondsValue (optional) An integer representing the milliseconds (0 to 999). If not provided, milliseconds will be set to 0.
    • If -1 is provided, it will result in the last millisecond of the previous second.
    • If 1000 is provided, it will result in the first millisecond of the next second.

Return Value

This method returns the number of milliseconds between midnight of January 1, 1970, and the updated date and time.

Example 1

In the following example, we are using the JavaScript Date setUTCHours() method to set the hour to 10, according to UTC time −

<html>
<body>
<script>
   const currentDate = new Date();
   currentDate.setUTCHours(10);

   document.write('Updated Date:  ', currentDate);
</script>
</body>
</html>

Output

This program returns the time 5 hours ahead of 10 hours (UTC). Minutes will not be 30 ahead because, we are not setting minutes here.

Example 2

Here, we are setting the hour to 12 (according to UTC) and returning it using the getUTCHours() method −

<html>
<body>
<script>
   const currentDate = new Date("December 25, 2023, 10:15:00");
   currentDate.setUTCHours(12);

   document.write('Updated UTC hour:  ', currentDate.getUTCHours());
</script>
</body>
</html>

Output

The above program returns 12 as hour.

Example 3

Here, we are providing a specific date to the Date object. Then, we are setting hour as 15, according to UTC time −

<html>
<body>
<script>
   const currentDate = new Date("December 25, 2023, 10:15:00");
   currentDate.setUTCHours(15); // Set the hours to 20 (According to UTC)

   document.write("Updated Date:  ", currentDate);
</script>
</body>
</html>

Output

This program returns the time 5 hours ahead of 15 hours (UTC). Minutes will not be 30 ahead because, we are not setting minutes here.

Example 4

In the below example, we are setting hour as 11 and 30 as minutes, according to UTC −

<html>
<body>
<script>
   const currentDate = new Date("December 25, 2023, 10:15:00");
   currentDate.setUTCHours(11, 30); // Set the hours to 11 and the minutes to 30 (According to UTC)

   document.write("Updated Date:  ", currentDate);
</script>
</body>
</html>

Output

This program returns the time 5 hours and 30 minutes ahead of 11 hours 30 minutes (UTC).

Example 5

Here, we are setting hours, minutes, and seconds according to UTC −

<html>
<body>
<script>
   const currentDate = new Date("December 25, 2023, 10:15:00");
   currentDate.setUTCHours(11, 20, 30); 

   document.write("Updated Date:  ", currentDate);
</script>
</body>
</html>

Output

The above program will return "Mon Dec 25 2023 16:50:30 GMT+0530 (India Standard Time)" as output.

Advertisements