Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert provided number of hours into days hours minutes in javascript?

I have an input hours which will be 0 to any hours, for example

25 hours... I need an output in days hours and minutes: 1 day 1 hour 0 min

I have tried and it returns days but help me to convert to days hrs and min var flag_hours = 25;

if(flag_hours >=24)
{
     var totaldays = flag_bookbefore/24;
     alert(totaldays);
}

which gives output as : 1.0416666666666667

Thanks in advance!

like image 942
john z Avatar asked Oct 23 '25 05:10

john z


1 Answers

Try this function: It gives you access to days hours and minutes separately.

function SplitTime(numberOfHours){
    var Days=Math.floor(numberOfHours/24);
    var Remainder=numberOfHours % 24;
    var Hours=Math.floor(Remainder);
    var Minutes=Math.floor(60*(Remainder-Hours));
    return({"Days":Days,"Hours":Hours,"Minutes":Minutes})
}

var hours=27.3
    var timeResult=SplitTime(hours)
    console.log("27.3 hours translate to  "+timeResult.Days+"Days "+timeResult.Hours+"Hours and "+timeResult.Minutes+"Minutes.")
like image 103
Paul Ghiran Avatar answered Oct 24 '25 20:10

Paul Ghiran



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!