Explain Codes LogoExplain Codes Logo

Get hours difference between two dates in Moment Js

javascript
moment-js
date-difference
time-zones
Anton ShumikhinbyAnton Shumikhin·Jan 9, 2025
TLDR

Detonate your deadline dilemmas with the .diff() function in Moment.js:

// Whoever said time was relative didn't use Moment.js! const diffHours = moment(endDate).diff(moment(startDate), 'hours'); console.log(diffHours); // The whole hours difference, laid bare.

However, for those fractional needs, duration objects have gotten your back with their .asHours() method:

// Who needs a fraction when you can have the whole pie? const duration = moment.duration(moment(endDate).diff(moment(startDate))); const diffFractionalHours = duration.asHours(); console.log(diffFractionalHours); // Harvest the decimals, reap the precision!

Juggle with time zones

Playing multi-time zone hopscotch? Moment Timezone is here for the rescue:

// New York, LA, and a cup of Moment Timezone flavored Java(script)! let startDate = moment.tz("2023-05-25 12:00", "America/New_York"); let endDate = moment.tz("2023-05-26 16:00", "America/Los_Angeles"); let diffHours = endDate.diff(startDate, 'hours'); console.log(diffHours); // Time zones? More like boring zones!

Time travel with fromNow() and from()

Unshackle the chains of calendar with fromNow() or from() for past date calculations:

// Because, who wouldn't want a time-machine! let pastDate = moment("2023-01-01"); console.log(pastDate.fromNow()); // Relive the nostalgia!

However, for traditional hour difference calculations for past dates, stick with our trusty .diff() and .asHours():

// Let's go back to the future, shall we! let pastDate = moment("2023-01-01"); let diffHours = moment().diff(pastDate, 'hours'); console.log(diffHours); // Tempus fugit, but not in our console.

Chain of methods

Because why write two lines of code, when one suffices?

// Coding efficiency for .diff(), at a coffee shop near you! let hoursDifference = moment(endDate).subtract(1, 'days').diff(moment(startDate), 'hours'); // Day subtracted, hour difference calculated, job application sent!

Code like a Pro

Display dates your way

With .format(), you're the Picasso of date presentation:

// Why do today what you can .format() for tomorrow. console.log(endDate.format('MMMM Do YYYY, h:mm:ss a')); // "May 26th 2023, 4:00:00 pm"

Daylight Saving Time (DST)

Because we all need that extra hour of sleep:

// Because, who really understands DST? (Correct answer: Moment JS!) let startDSTDate = moment('2023-03-13T02:00:00-0400'); let endDSTDate = moment('2023-03-13T03:00:00-0400'); let diffDSTHours = endDSTDate.diff(startDSTDate, 'hours'); console.log(diffDSTHours); // Just another DST miracle!

Always use the latest Moment.js

Get the latest and greatest version of Moment.js:

<script src="https://cdn.jsdelivr.net/momentjs/latest/moment.min.js"></script>

Comments and naming

Clear names improve code understanding and reduce forehead wrinkles:

// Code reluctant to age, with variable Botox! let meetingStart = moment('2023-06-01T09:00:00'); let meetingEnd = moment('2023-06-01T17:00:00'); let meetingDurationHours = meetingEnd.diff(meetingStart, 'hours'); console.log(meetingDurationHours); // Your 9 to 5, in console log!