Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

225
Views
Find the mid time value between two time values using momentjs

Assume I have two time values as startTime and endTime. I want to find the exact mid value of this time objects. Example: if startTime is 10:30 and endTime is 11:30, I need 11:00 as the midpoint value.

How to solve this using momentjs in JavaScript?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

The middle between two dates is just half of the difference between the dates added to the smaller one.

The difference should be pretty straight forward:

Math.abs(moment(a).diff(b))

Math.abs() removes the minus from negative numbers (aka if a < b).

Calculating the middle has two steps too, first divide the difference by two, then add it to the smaller date (Math.min()):

diff/2+Math.min(moment(a).valueOf(),moment(b).valueOf())

moment(n).valueOf() turns your Date strings into comparable integers.

function middleDate(a,b) {
  let diff = Math.abs(moment(a).diff(b))
  let middle = diff/2+Math.min(moment(a).valueOf(),moment(b).valueOf())
  return moment(middle)
}

console.log(middleDate("2022-01-27T10:30:00Z", "2022-01-27T11:30:00Z"))
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>

If you only want the hours and minutes, you can just format the returned moment object (and use moment.utc() to remove the timezone):

function middleDate(a,b) {
  let diff = Math.abs(moment(a).diff(b))
  let middle = diff/2+Math.min(moment(a).valueOf(),moment(b).valueOf())
  return moment(middle)
}

console.log(
  moment.utc(middleDate("2022-01-27T10:30:00Z", "2022-01-27T11:30:00Z")).format("hh:mm")
) 
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>

about 4 years ago · Juan Pablo Isaza Report

0

Here is how i solved it

(For my particular case that deals with time only. For more generic cases, you can use the answer above)

import moment from 'moment';
export const middleDate = (startTime, endTime) => {
  let duration = moment.duration(moment(endTime, 'HH:mm:ss').diff(moment(startTime, 'HH:mm:ss'))); //get Total Interval
  let midInterval = duration.asHours() / 2; // find half of the interval
  return moment(moment(startTime, 'HH:mm:ss')).add(midInterval, 'hour').format('HH:mm'); //add the mid interval to the startTime we have
};
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!