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

Converting decimal to hours and minutes - Javascript - Stack Overflow

programmeradmin1浏览0评论

I just can't figure why this doesn't work for some odd values. For example when trying to convert 22.68 to hours and minutes the output is 22:40.800000000000004 (Seconds shouldn't even appear)

if (str_HR_PER_WEEK.indexOf('.') > -1)
    {
        var str_HR_PER_WEEK_hrs = str_HR_PER_WEEK.substring(0 , str_HR_PER_WEEK.indexOf('.'));
        var str_HR_PER_WEEK_mins = str_HR_PER_WEEK.substring(str_HR_PER_WEEK.indexOf('.') + 1);
        var float_HR_PER_WEEK_mins = parseFloat("0." + (str_HR_PER_WEEK_mins), 10);
        var float_HR_PER_WEEK_mins_actual = float_HR_PER_WEEK_mins * 60;
        float_HR_PER_WEEK_mins_actual = float_HR_PER_WEEK_mins_actual.toString();

        tables.CURRENT_EMPLOYEES.HOURS_PER_WEEK.value = getTwoDigitTime(str_HR_PER_WEEK_hrs) + ":" + getTwoDigitTime(float_HR_PER_WEEK_mins_actual);
    }
else
    {
        tables.CURRENT_EMPLOYEES.HOURS_PER_WEEK.value = str_HR_PER_WEEK;
    }

I just can't figure why this doesn't work for some odd values. For example when trying to convert 22.68 to hours and minutes the output is 22:40.800000000000004 (Seconds shouldn't even appear)

if (str_HR_PER_WEEK.indexOf('.') > -1)
    {
        var str_HR_PER_WEEK_hrs = str_HR_PER_WEEK.substring(0 , str_HR_PER_WEEK.indexOf('.'));
        var str_HR_PER_WEEK_mins = str_HR_PER_WEEK.substring(str_HR_PER_WEEK.indexOf('.') + 1);
        var float_HR_PER_WEEK_mins = parseFloat("0." + (str_HR_PER_WEEK_mins), 10);
        var float_HR_PER_WEEK_mins_actual = float_HR_PER_WEEK_mins * 60;
        float_HR_PER_WEEK_mins_actual = float_HR_PER_WEEK_mins_actual.toString();

        tables.CURRENT_EMPLOYEES.HOURS_PER_WEEK.value = getTwoDigitTime(str_HR_PER_WEEK_hrs) + ":" + getTwoDigitTime(float_HR_PER_WEEK_mins_actual);
    }
else
    {
        tables.CURRENT_EMPLOYEES.HOURS_PER_WEEK.value = str_HR_PER_WEEK;
    }
Share Improve this question edited Sep 12, 2018 at 10:14 Ran 3331 gold badge4 silver badges17 bronze badges asked Sep 12, 2018 at 10:11 RenjiRenji 531 silver badge8 bronze badges 3
  • What time should 22.68 mean? Is it 22 hours and 40.8min? Which values do work, which don't? – lars k. Commented Sep 12, 2018 at 10:16
  • Maybe this answer will help you: stackoverflow./a/47901325/3960931 – Štefan Ondáš Commented Sep 12, 2018 at 10:18
  • @larsk. 22.68 should return 22.40 – Renji Commented Sep 12, 2018 at 10:21
Add a ment  | 

3 Answers 3

Reset to default 5

You have to ways to achieve that,

one, do the calculations yourself:

var decimalTimeString = "1.6578";
var decimalTime = parseFloat(decimalTimeString);
decimalTime = decimalTime * 60 * 60;
var hours = Math.floor((decimalTime / (60 * 60)));
decimalTime = decimalTime - (hours * 60 * 60);
var minutes = Math.floor((decimalTime / 60));
decimalTime = decimalTime - (minutes * 60);
var seconds = Math.round(decimalTime);
if(hours < 10)
{
    hours = "0" + hours;
}
if(minutes < 10)
{
    minutes = "0" + minutes;
}
if(seconds < 10)
{
    seconds = "0" + seconds;
}
alert("" + hours + ":" + minutes + ":" + seconds);

Two, use built in function to convert to string and then to hh:mm:

var decimalTimeString = "1.6578";
var n = new Date(0,0);
n.setSeconds(+decimalTimeString * 60 * 60);
n.setMinutes(+decimalTimeString * 60); 
var result = n.toTimeString().slice(0, 5);
document.write(result);

I've got a neat function to do just that:

function hoursToHHMM(hours) {
    var h = String(Math.trunc(hours)).padStart(2, '0');
    var m = String(Math.abs(Math.round((hours - h) * 60))).padStart(2, '0');
    return h + ':' + m;
}

It handles negative values as a bonus.

Usage is trivial:

var hours = -7.33333;
console.log(hoursToHHMM(hours));

Results in: -07:20

You can play with it here: https://jsfiddle/r150c2me/

Here's my TypeScript solution, if anyone is interested:

  /**
   * A function that accepts decimal minutes and formats them to either `HH:MM:SS` or, if less than 60, `MM:SS`.
   *
   * @param decimalMinutes
   */
  convertDecimalMinutesToTime(decimalMinutes: number): string {
    if (!decimalMinutes || decimalMinutes <= 0) return '00:00';

    const hours: number = Math.floor(decimalMinutes / 60);
    const minutes: number = Math.floor(decimalMinutes % 60);
    const seconds: number = Math.floor((decimalMinutes * 60) % 60);

    const formattedHours: string = hours < 10 ? `0${hours}` : `${hours}`;
    const formattedMinutes: string = minutes < 10 ? `0${minutes}` : `${minutes}`;
    const formattedSeconds: string = seconds < 10 ? `0${seconds}` : `${seconds}`;

    if (hours >= 1) {
      return `${formattedHours}:${formattedMinutes}:${formattedSeconds}`;
    } else {
      return `${formattedMinutes}:${formattedSeconds}`;
    }
  }
发布评论

评论列表(0)

  1. 暂无评论