Home > Mobile >  calculating hours between two dates and time in javascript
calculating hours between two dates and time in javascript

Time:12-01

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

CodePudding user response:

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('hours: '   duration.asHours(), '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>
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

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)

CodePudding user response:

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>
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

  • Related