How to add 2 hours to a JavaScript Date object?

Front End TechnologyJavascriptWeb Development

In this tutorial, we will learn how to add 2 Hours to a JavaScript Date object. Here we will discuss two methods which are following.

  • Using the getTime() Method
  • Using the setHours( ) Method

Using the getTime( ) Method

JavaScript date getTime() method returns the numeric value corresponding to the time for the specified date according to universal time. The value returned by the getTime() method is the number of milliseconds since 1 January 1970 at 00:00:00.

Syntax

Following the syntax for the getTime() method −

Date.getTime()

Approach

To add 2 hours to the Date Object first, we get the current time by using the Date.getTime() method and then add 2 hour’s milliseconds value (2 * 60 * 60 * 1000) to it and pass the added value to the Date Object.

Example 1

In this example, we are adding 2 hours to the current time using the getTime() method.

<html> <head> <title>Example: add 2 hrs to JavaScript Date Object</title> </head> <body> <h2> Add 2 hours to the JavaScript Date object using getTime() method </h2> <p> Click on the button to add 2 Hours to the current date/time. </p> <button onclick="add()">Click Me</button> <p id="currentTime">Current Time : </p> <p id="updatedTime">Updated Time: </p> </body> <script> // Code the show current time let ct = document.getElementById("currentTime") setInterval(() => { let currentTime = new Date().getTime(); ct.innerText = "Current Time : " + new Date(currentTime).toLocaleTimeString() }, 1000) // Code to add 2 hours to current Time let ut = document.getElementById("updatedTime") function add() { setInterval(() => { let currentTime = new Date().getTime(); let updatedTIme = new Date(currentTime + 2 * 60 * 60 * 1000); ut.innerText = "Updated Time : " + updatedTIme.toLocaleTimeString() }, 1000) } </script> </html>

Using the setHours() method

JavaScript date setHours() method sets the hours for a specified date according to local time.

Syntax

Date.setHours(hours, minutes, seconds, ms)

Here parameter hours and other parameters are discussed below.

Parameters

  • hours − An integer between 0 and 23, representing the hour.

  • minutes − An integer between 0 and 59, representing the minutes.

  • seconds − An integer between 0 and 59, representing the seconds. If you specify the seconds parameter, you must also specify the minutes.

  • ms − A number between 0 and 999, representing the milliseconds. If you specify the ms parameter, you must also specify the minutes and seconds.

Parameters except hours are optional.

Approach

To add 2 hours into the Date object using the setHours() method first we get the value of hours of the current time and then add 2 into it and pass the added value to the setHours( ) method.

Example 2

In this example, we are adding 2 Hours to the current time/time.

<html> <head> <title>Program to add 2 hours to Date object</title> </head> <body> <h2> Add 2 hours to the JavaScript Date object using setHours( ) method </h2> <p> Click on the button to add 2 Hours to the current date/time.</p> <button onclick="add()">Click Me</button> <p id="currentTime">Current Time : </p> <p id="updatedTime">Updated Time: </p> </body> <script> // Code the show current time let ct = document.getElementById("currentTime") setInterval(() => { let currentTime = new Date().getTime(); ct.innerText = "Current Time : " + new Date(currentTime).toLocaleTimeString() }, 1000) // Code to add 2 hours to current Time let ut = document.getElementById("updatedTime") function add() { setInterval(() => { let dt = new Date(); dt.setHours(dt.getHours() + 2); ut.innerText = "Updated Time : " + dt.toLocaleTimeString(); }, 1000) } </script> </html>

In summary, we discussed two approaches to add 2 hours to a JavaScript Date object. The first one is using the getTime() method and the second is using the setHours() method.

raja
Updated on 22-Aug-2022 07:56:00

Advertisements