How to use setUTCHours in JavaScript

In this article, I will explain use of setUTCHours() method in JavaScript date object.
  • 1754

JavaScript setUTCHours() method

  • setUTCHours() method of date object is used to sets the hour for a specified date according to the UTC.

  • setUTCHours() method can also be used to set the minutes, seconds and milliseconds.

  • UTC time is the same as GMT time.

Syntax

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

  • Hours : Hours is required parameter. An integer is representing the hour and it's value between 0 and 23.

  • Min : Min is optional parameter. An integer is representing the minutes and it's value between 0 and 59.

  • Sec : Sec is optional parameter. An integer is representing the seconds and it's value between 0 and 59,. If you given the secondsValue parameter, you must also give the minutesValue.

  • Mili : Mili is optional parameter. A number is representing the milliseconds and it's value between 0 and 999. If you give the Mili parameter, you must also give the Minute and Sec.

Example

In the following example show to setUTCHours() method set the time to 72 hours ago, using UTC methods.

<!DOCTYPE html>

<html>

<body>

<h3 style="color:Maroon ">JavaScript setUTCHours() Method Example</h3>

<p id="demo">Click for display a date after set it to 72 hours ago.</p>

<button onclick="myFunction()">Try it</button>

<script type="text/javascript">

    function myFunction() {

        var d = new Date();

        d.setUTCHours(d.getUTCHours() - 72);

        var x = document.getElementById("demo");

        x.innerHTML = "After changing time, according to UTC : " + d;

    }

</script>

</body>

</html>

Output1

UTCHours 1.jpg

Output2

UTCHours 2.jpg

You may also want to read these related articles here

Ask Your Question 

Got a programming related question? You may want to post your question here

Programming Answers here

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.