JavaScript Date.setUTCHours() Method

Date.setUTCHours() method in JavaScript; Through this tutorial, i am going to show you how to set utc hours in JavaScript using setUTCHours with help of examples.

JavaScript setUTCHours() Method

The setUTCHours() is an inbuild javascript method, which is used to sets the hour of a date object, according to the universal time (UTC).

Note: This method accepts four-parameter values. The first one is required and the remaining parameters are optional in this function.

Syntax of JavaScript setUTCHours() Method

Date.setUTCHours(hour, min, sec, millisec);

Parameter of JavaScript setUTCHours() Method

ParameterDescription
hourThis is a first parameter and required. In this method, it allows integer value between 0 to 23.
If you will pass -1
1. -1 will result in the last hour of the previous day
If you will pass 24
1. 24 will result in the first hour of the next day
minThis is a second parameter and optional. You can pass values integer value from 0-59 in this parameter
sec This is a third parameter and optional. You can pass values integer value from 0-59 in this parameter
millisec This is a fourth parameter and optional. You can pass values integer value from 0-999 in this parameter

If you want to pass

1. hours in setUTCHours() method like:

 Date.setUTCHours(hour)

2. hours and minutes in setUTCHours() method like:

 Date.setUTCHours(hour, min)

3. hours, minutes and seconds in setUTCHours() method like:

 Date.setUTCHours(hour, min, sec)

Example 1 – JavaScript setUTCHours() Method

See the following example using setUTCHours method; as shown below:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>javascript date set utc hours</title>
</head>
<body>
  <script type = "text/javascript">
     var date = new Date();
     date.setUTCHours(12);
     document.write( "javascript date set utc hours: " + date );
  </script>  
</body>
</html>

Output of the above code is:

javascript date set utc hours

Example 2 – JavaScript setUTCHours() Method

Let’s take an example using hours and minutes with setUTCHours method; as shown below:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>javascript date set utc hours with minutes</title>
</head>
<body>
  <script type = "text/javascript">
     var date = new Date();
     date.setUTCHours(12, 40);
     document.write( "javascript date set utc hours with minutes: " + date );
  </script>  
</body>
</html>

Output of the above code is:

javascript date set utc hours with minutes

Example 3 – JavaScript setUTCHours() Method

Let’s take an third example of setUTCHours method, with hours, minutes and seconds is:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>javascript date set utc hours with minutes and seconds</title>
</head>
<body>
  <script type = "text/javascript">
     var date = new Date();
     date.setUTCHours(12, 40, 30);
     document.write( "javascript date set utc hours with minutes and seconds: " + date );
  </script>  
</body>
</html>

Output of the above code is:

javascript date set utc hours with minutes and seconds

Conclusion

In this javascript tutorial, you have learned how to sets a hours with year, minutes and seconds parameters using setUTCHours method with example.

Recommended JavaScript Tutorials

Be First to Comment

Leave a Reply

Your email address will not be published.