Skip to content

All topics  /  JavaScript

How to set Date in setUTCHours() Method in JavaScript?

JavaScript ·

This example is focused on javascript date setutchours() method with examples. In this article, we will implement a javascript date setutchours() method. we will help you to give example of date.prototype.setutchours() - javascript. I would like to show you javascript date setutchours() method. Alright, let’s dive into the steps.

The `setUTCHours()` method in JavaScript is used to set the hour value of a Date object according to Coordinated Universal Time (UTC). Here's how you can use it to set the date:

Example 1:


<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>How to set Date in setUTCHours() Method in JavaScript? - NiceSnippets.Com</title>
</head>
<body>
</body>
<script type="text/javascript">
    // Create a new Date object
    let date = new Date();
    // Set the hour value using setUTCHours()
    date.setUTCHours(10);
    // Output the updated date
    console.log(date);
</script>
</html>

In this example, we create a new Date object and then use the `setUTCHours()` method to set the hour value to 10. The `setUTCHours()` method takes an integer parameter representing the hour value (from 0 to 23) and updates the Date object accordingly.

Note that `setUTCHours()` modifies the Date object in place and returns the updated timestamp.