javascript date setUTCHours() Method With Examples

javascript date setUTCHours() Method With Examples

setUTCHours() Method JavaScript 

Definition:- 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

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

Parameter of 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

Here we will take first example of setUTCHours method, without passing minutes, seconds and milliseconds:

<!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>

Result of example – 1 is:

javascript date set utc hours

Example – 2

Here we will take second example of setUTCHours method, with hours and minutes is:

<!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>

Result of example – 2 is:

javascript date set utc hours with minutes

Example – 3

Here we will take 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>

Result of example – 3 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.

You may like

  1. JavaScript: Date setUTCDate() Method
  2. Set UTC Month In javaScript
  3. setUTCFullYear() Method JavaScript With Examples
  4. JavaScript: Date.getUTCDate() Method
  5. getUTCmonth Method javaScript With Example
  6. javaScript Digital Clock with date
  7. JavaScript: Set Date Methods
  8. String Contains JavaScript | String includes() Method
  9. JavaScript Get Date Methods
  10. JavaScript Operators With Examples
  11. Sorting Using JavaScript Array Sort() Method
  12. JavaScript Replace String With Examples
  13. JavaScript Array splice() Method By Example
  14. JavaScript: Clone an Array & Object By Example
  15. Check the Object is Array or not in javascript

If you have any questions or thoughts to share, use the comment form below to reach us.

Leave a Reply

Your email address will not be published. Required fields are marked *