Navigate back to the homepage

How to get hours between two dates in javascript?

Infinitbility
JavaScript
June 8th, 2022 · 1 min read

Hi Friends 👋,

Welcome To Infinitbility! ❤️

To get hours between two dates,

  1. Get both dates as a time using the getTime() method.
  2. subtract them which you get using the getTime() method.
  3. Devied your output time with / 36e5 ( 36e5 is the scientific notation for 60601000 )

It will return your difference of dates in number of hours.

The getTime() method returns the number of milliseconds since the ECMAScript epoch.

You can use this method to help assign a date and time to another Date object. This method is functionally equivalent to the valueOf() method.

Today, I’m going to show How do I get hours between two dates in javascript, here I will use the javascript getTime() method and the above-mentioned conditions to get hours between the start and end date time.

Let’s start the today’s tutorial How do you get hours between two dates in javascript?

In the following example, we are going to do

  1. Create two dates starting and ending date
  2. Create a function that accepts a date and returns hours
  3. Use a function for multiple date examples

let’s write the code.

1function getHours(startingDate, endingDate) {
2 return parseInt(Math.abs(endingDate.getTime() - startingDate.getTime()) / 36e5);
3}
4const startDate = new Date("2021-08-25 07:41:07");
5const endDate = new Date("2021-08-25 09:50:07");
6getHours(startDate, endDate); // ✅ 2

In the above program, we have created a custom function getHours() and pass dates to get a mumber of hours.

let’s check the output.

javascript, get hours between two dates example
javascript, get hours between two dates example

I hope it’s help you, All the best 👍.

Follow me on Twitter

Join our email list and get notified about new content

No worries, I respect your privacy and I will never abuse your email.

Every week, on Tuesday, you will receive a list of free tutorials I made during the week (I write one every day) and news on other training products I create.

Looking For React Native Tutorial?

we are trying to create the best Tutorial for react native developers.

When you want a daily updates about React Native Tutorial or infinitbility update subscribe to our newsletter.

Read React Native Tutorial

Request New Tutorial or Article on mail [email protected]

Guide book

Guidebook: The Javascript Monorepo Photoshop Guide

Tutorials

Categories

More articles from Infinitbility

How to get minutes between two dates in javascript?

Example of get minutes between two dates in javascript with step by step guide.

June 7th, 2022 · 1 min read

How to check if a string is alphanumeric in JavaScript?

Example of check if a string contains only alphabets and numbers in javascript with step by step guide.

June 6th, 2022 · 1 min read
© 2020–2022 Infinitbility
About
Link to $https://medium.com/infinitbilityLink to $https://www.facebook.com/InfinitbilityLink to $https://github.com/infinitbilityLink to $https://twitter.com/infinitbilityLink to $https://www.buymeacoffee.com/infinitbilityLink to $mailto:[email protected]