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

Converting Float to Time in Javascript - Stack Overflow

programmeradmin3浏览0评论

I am having a three String or Float values say 9.30, 8.00 and 0.40 as Total_hour, Paid_hour, Extra_hour

These should be actually 9 hours 30 minutes, 8 hours 0 minutes, 0 hours 40 minutes.

Question 1) How to convert 9.30 to 9 hours 30 minutes

Question 2) Later want to Subtract and get Remaining Hour = Total_hour-Paid_Hour-Extra_hour

Later the answer Remaining Hour should be in float

I am having a three String or Float values say 9.30, 8.00 and 0.40 as Total_hour, Paid_hour, Extra_hour

These should be actually 9 hours 30 minutes, 8 hours 0 minutes, 0 hours 40 minutes.

Question 1) How to convert 9.30 to 9 hours 30 minutes

Question 2) Later want to Subtract and get Remaining Hour = Total_hour-Paid_Hour-Extra_hour

Later the answer Remaining Hour should be in float

Share Improve this question edited Dec 20, 2017 at 8:27 Abubakker Moallim asked Dec 20, 2017 at 7:36 Abubakker MoallimAbubakker Moallim 1,6581 gold badge13 silver badges22 bronze badges 5
  • 2 Is this Javascript or Python? – Charlie Fish Commented Dec 20, 2017 at 7:36
  • I want to in Javascript and in last if it not possible in javascript I can do it in Python – Abubakker Moallim Commented Dec 20, 2017 at 7:37
  • Now what happens if your number is 0.90? What happens then? – Charlie Fish Commented Dec 20, 2017 at 7:38
  • I cant be like that. Maximum it will 0.60 not more than that – Abubakker Moallim Commented Dec 20, 2017 at 7:39
  • 3 Just out of curiosity I want to know why people are downvoting this question – Abubakker Moallim Commented Dec 20, 2017 at 8:28
Add a ment  | 

4 Answers 4

Reset to default 6

The following javascript snippet converts a given float to hours and minutes. Source float to time

function convertNumToTime(number) {
    // Check sign of given number
    var sign = (number >= 0) ? 1 : -1;

    // Set positive value of number of sign negative
    number = number * sign;

    // Separate the int from the decimal part
    var hour = Math.floor(number);
    var decpart = number - hour;

    var min = 1 / 60;
    // Round to nearest minute
    decpart = min * Math.round(decpart / min);

    var minute = Math.floor(decpart * 60) + '';

    // Add padding if need
    if (minute.length < 2) {
    minute = '0' + minute; 
    }

    // Add Sign in final result
    sign = sign == 1 ? '' : '-';

    // Return concated hours and minutes
    return sign + hour + ':' + minute;
}

console.log(convertNumToTime(11.15));

Output

11:09

This should work.

You just need to convert to ms:

let timefloat = 9.3;


function convertToMs(timefloat) {
  // Get the minutes portion
  let remainder = timefloat % 1;

  // Convert into ms
  let minutes = remainder * 100 * 60 * 1000;

  // Get the number of hours and convert to ms 
  let hours = (timefloat - remainder) * 60 * 60 * 1000;

  return minutes + hours;
}

// Convert back to float format
function convertToFloat(date) {
  let hours = date.getUTCHours();
  let mins = date.getUTCMinutes();
  return hours + (mins / 100);
}


// Log the result
console.log(new Date(convertToMs(9.3)).toUTCString());
console.log(new Date(convertToMs(8.0)).toUTCString());
console.log(new Date(convertToMs(9.3) - convertToMs(8.0)).toUTCString());

let diff = convertToMs(9.3) - convertToMs(8.0);
console.log(convertToFloat(new Date(diff)))

First convert the number in minutes and then do your subtraction. Then convert your output to hours.

var Total_hour = '9.30',
    Paid_hour = '8.00',
    Extra_hour = '0.40';

var conversionInMinutes = hour => Math.floor(hour) * 60 + (hour - (Math.floor(hour))) * 100;
var conversionInHours = min => Math.floor( min/60 ) + min % 60 / 100;
var Remaining_hour = conversionInMinutes(Total_hour) - conversionInMinutes(Paid_hour) - conversionInMinutes(Extra_hour);

console.log(conversionInHours(Remaining_hour).toFixed(2));

function doTime(input)
{
    input = input.toString()
    inputs = input.split(".")
    return (inputs[0] + "Hour and" + inputs[1] + "minutes")

}

doTime("9:22")

function substract2

function subtract2(a , b){
     a = input.toString()
     arrayA = input.split(".")
     b = input.toString()
     arrayB = input.split(".")
     h = parseInt(arrayB[0]) - parseInt(arrayA[0])
     h <0 ? h+=12/*or 24*/ :h=h
     m = parseInt(arrayB[1]) - parseInt(arrayA[1])
     if(m<0){h-- ; m+=60}
     return h.toString() + ":" + m.toString()
}
发布评论

评论列表(0)

  1. 暂无评论