• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / JavaScript / Add Hours to a Date Using JavaScript

Add Hours to a Date Using JavaScript

January 21, 2022 Leave a Comment

To add hours to a date, we can use the JavaScript getTime() method. The getTime() method converts dates into the number of milliseconds since January 1st, 1970. Since there are 3600000 milliseconds in 1 hour, we can add multiples of 3600000 to our converted date to add as many hours as we want.

var date1 = new Date();
var dateToMilliseconds = date1.getTime();
var addedHours = dateToMilliseconds + (3600000*5);
//This will add 5 hours to our time.
var newDate = new Date(addedHours);
//This will create a new date that will be 5 hours ahead of the current date

We can also add negative milliseconds to subtract hours from a date. Below are some examples of adding hours to a date object with the help of the getTime() method.

var date1 = new Date();
var dateToMilliseconds = date1.getTime();
var result1 = date1;
var result2 = new Date(dateToMilliseconds+(3600000*5));
var result3 = new Date(dateToMilliseconds+(3600000*24));
var result4 = new Date(dateToMilliseconds+(3600000*24*30));
var result5 = new Date(dateToMilliseconds+(-3600000*5));

Which would result in the following:

result1 = Fri Jan 21 2022 12:44:54 GMT-0500 (Eastern Standard Time);
result2 = Fri Jan 21 2022 17:44:54 GMT-0500 (Eastern Standard Time);
result3 = Sat Jan 22 2022 12:44:54 GMT-0500 (Eastern Standard Time);
result4 = Sun Feb 20 2022 12:44:54 GMT-0500 (Eastern Standard Time);
result5 = Fri Jan 21 2022 07:44:54 GMT-0500 (Eastern Standard Time);

Add Hours to a Date Using JavaScript with a Click

In this example, we will let the user add hours to the current date using an input field. We will use the getTime() method to convert dates to milliseconds like we did above.

Here is our HTML setup:

<p>How many hours do you want to add to the current date and time?</p>
<input id="userVal" type="text" value="">
<input id="submitNum" type="submit" onclick="addHours()" value="Submit">
<div id="results"></div>

Below is the JavaScript code which will take the user input using the onclick event and run the function below. The function will add the hours to the current date like we did above. We will then update the #results div using the textContent property.

function addHours() {
  var date1 = new Date();
  var dateToMilliseconds = date1.getTime();
  var userHours = document.getElementById("userVal").value;
  var addedHours = dateToMilliseconds + (3600000*userHours);
  var newDate = new Date(addedHours);
  document.getElementById("results").textContent = newDate;
}

The final code and output for this example is below:

Code Output:

How many hours do you want to add to the current date and time?


Full Code:

<p>How many hours do you want to add to the current date and time?</p>
<input id="userVal" type="text" value="">
<input id="submitNum" type="submit" onclick="addHours()" value="Submit">
<div id="results"></div>

<script>

function addHours() {
  var date1 = new Date();
  var dateToMilliseconds = date1.getTime();
  var userHours = document.getElementById("userVal").value;
  var addedHours = dateToMilliseconds + (3600000*userHours);
  var newDate = new Date(addedHours);
  document.getElementById("results").textContent = newDate;
}

</script>

Hopefully this article has been useful in helping you understand how to Add Hours to a Date Using JavaScript.

Other Articles You'll Also Like:

  • 1.  JavaScript toLocaleString – Display the Date and Time Using JavaScript
  • 2.  JavaScript Check if Attribute Exists in HTML Element
  • 3.  lastIndexOf JavaScript – Get the Last Position of a String in Another String
  • 4.  JavaScript asinh – Find Hyperbolic Arcsine of Number Using Math.asinh()
  • 5.  How to Create a New h1 Element With JavaScript
  • 6.  JavaScript sinh – Find Hyperbolic Sine of Number Using Math.sinh()
  • 7.  Convert an Array to Set in JavaScript
  • 8.  How to Create a JavaScript Count Up Timer
  • 9.  JavaScript Round to Nearest 10
  • 10.  Using JavaScript to Hide Element by Class

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

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

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy