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

715
Views
calculating hours between two dates and time in javascript

I am trying to calculate total hours and minute different two different date and time:

below is my code:

let startShiftTime = moment(StartTime, ['DD-MM-YYYY h:mm:ss A'])
    .utcOffset(0, false)
    .format('DD-MM-YYYY hh:mm:ss A');

let endShiftTime = moment(EndTime, ['DD-MM-YYYY h:mm:ss A'])
    .utcOffset(0, false)
    .format('DD-MM-YYYY hh:mm:ss A');

var TotalSeconds = moment(startShiftTime, 'DD-MM-YYYY hh:mm:ss A')
    .utcOffset(0, false)
    .diff(
        moment(endShiftTime, 'DD-MM-YYYY hh:mm:ss A').utcOffset(0, false),
        'seconds',
    );

var hours = Math.floor(TotalSeconds / 3600);
var minutes = Math.floor((TotalSeconds / 60) % 60);
console.log(
    'startShiftTime: ',
    startShiftTime,
    ' endShiftTime: ',
    endShiftTime,
    ' hours: ',
    hours,
    ' minutes: ',
    minutes,
);

I am getting hours and minutes in all cases apart from case time difference is 1 hours or else

console output:

startShiftTime:  02-12-2021 09:00:00 AM  endShiftTime:  01-12-2021 09:30:00 PM  hours:  11  minutes:  30
startShiftTime:  02-12-2021 04:30:00 PM  endShiftTime:  02-12-2021 05:00:00 PM  hours:  -1  minutes:  -30

see in the second output... time difference is only 30 mins.. but in hours I am getting as 1

I am not able to understand where I am making mistake

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

0

You don't need to do those calculations by yourself, but you can use the moment.duration API:

const startShiftTime = moment('01-12-2021 09:00:00 AM', 'DD-MM-YYYY hh:mm:ss A');
const endShiftTime = moment('01-12-2021 09:30:00 AM', 'DD-MM-YYYY hh:mm:ss A');

const duration = moment.duration(startShiftTime.diff(endShiftTime));

console.log('as hours: ' + duration.asHours(), 'as minutes: ' + duration.asMinutes());
console.log('hours: ' + duration.hours(), 'minutes: ' + duration.minutes());
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>

PS: You get negative values because the difference is done on the start time, which is smaller than the end time. If you want positive values then you just inverse the diff: endShiftTime.diff(startShiftTime)

about 4 years ago · Juan Pablo Isaza Report

0

Why are startShiftTime and endShiftTime formatted strings instead of instances of moment. You end up parsing them again later.

You are calculating the difference between times the wrong way round by it should be.

endShiftTime
    .diff(startShiftTime,'seconds')

This then leads to the obvious question, why is the first example calculating correctly... It because the end shift time is before the start time, this doesn't make any sense.

Swapping the start and end times in your first example then result in the correct answer in both instances.

function calculate(StartTime, EndTime) {

  let startShiftTime = moment(StartTime, ['DD-MM-YYYY h:mm:ss A'])
    .utcOffset(0, false);

  let endShiftTime = moment(EndTime, ['DD-MM-YYYY h:mm:ss A'])
    .utcOffset(0, false);

  var TotalSeconds = endShiftTime
    .diff(startShiftTime,'seconds');
    
  var hours = Math.floor(TotalSeconds / 3600);
  var minutes = Math.floor((TotalSeconds / 60) % 60);
  console.log(`startShiftTime: ${startShiftTime.format('DD-MM-YYYY h:mm:ss A')} endShiftTime: ${endShiftTime.format('DD-MM-YYYY h:mm:ss A')} hours: ${hours} minutes: ${minutes}`);
}

calculate('01-12-2021 09:30:00 PM', '02-12-2021 09:00:00 AM');
calculate('02-12-2021 04:30:00 PM', '02-12-2021 05:00:00 PM');
<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
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!