最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

datetime - How to convert provided number of hours into days hours minutes in javascript? - Stack Overflow

programmeradmin5浏览0评论

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!

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!

Share Improve this question edited Jul 4, 2016 at 21:06 user557846 asked Jul 4, 2016 at 21:02 john zjohn z 911 gold badge1 silver badge8 bronze badges 2
  • Math.floor(number) returns the nearest integer. In your example it'll return 1 which is one day – Hearner Commented Jul 4, 2016 at 21:10
  • Division will give you the number of days and for the rest you could use the modulo operator to calculate the leftover hours and mins. – theatlasroom Commented Jul 5, 2016 at 0:59
Add a ment  | 

3 Answers 3

Reset to default 10

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.")

Try this:

var days = Math.floor(flag_hours / 24);
var hours = Math.floor(flag_hours) % 24;
var minutes = (flag_hours - Math.floor(flag_hours)) * 60;

Not tested, might be horribly wrong.

Try this:

var hour = 47.5;
var day = 0;
var minute = parseInt((hour % 1)*60);
if (hour>24){
    day = parseInt(hour / 24);
    hour = parseInt(hour % 24);
}else{
    hour = parseInt(hour);
}
alert (day);
alert(hour);
alert(minute);

check it in jsFiddle

发布评论

评论列表(0)

  1. 暂无评论