JavaScript date setUTCHours method; In this tutorial, you will learn javaScript setUTCHours method with the help of 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
Parameter | Description |
---|---|
hour | This 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 |
min | This 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:
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:
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:
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
- JavaScript: Date setUTCDate() Method
- Set UTC Month In javaScript
- setUTCFullYear() Method JavaScript With Examples
- JavaScript: Date.getUTCDate() Method
- getUTCmonth Method javaScript With Example
- javaScript Digital Clock with date
- JavaScript: Set Date Methods
- String Contains JavaScript | String includes() Method
- JavaScript Get Date Methods
- JavaScript Operators With Examples
- Sorting Using JavaScript Array Sort() Method
- JavaScript Replace String With Examples
- JavaScript Array splice() Method By Example
- JavaScript: Clone an Array & Object By Example
- 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.