Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Round up duration between two times if difference exceeds 10 minutes

I have two dates which are generally the start_time and end_time. I want to take the difference between the times and round up the hours component if the minutes component is > 10 minutes. For example:

  • difference of 12 minutes - round up to 1 hour
  • difference of 1 hour 31 minutes - round up to 2 hours
  • difference of 2 hours 4 minutes - keep at 2 hours
  • difference of 3 hours 59 minutes - round up to 4 hours

So here is the code what I am able to achieve until now. I am finding the difference between the two dates and diving it by 3600000 and rounding it up but that is something like converting 1 hour 30 minutes to 2 hours.

 var duration = Math.round(moment(end_time).diff(moment(booking_result.start_time))  / 3600000);

Can anyone suggest a solution?

like image 667
sac Dahal Avatar asked Dec 13 '25 14:12

sac Dahal


1 Answers

As I see you're using momenjs then it's easy what you want to do:

var duration = moment.duration(end_time.diff(start_time));
var hours = duration.asHours();

that will give you the duration in hours with decimals, as I understand from your question you want to do is round this upwards to the nearest integer, but only when this difference has more than 10 min. in the minutes component so:

if (duration.minutes() > 10) {
    var result = Math.ceil(hours);
}
like image 151
Vi100 Avatar answered Dec 15 '25 03:12

Vi100



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!