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

Javascript convert HH:MM to decimal - Stack Overflow

programmeradmin0浏览0评论

I have a Javascript function that gives me the total hours worked in a day as HH:MM. I want to convert those numbers to decimals so I can add them up. I just seem to be getting errors at the moment:

function timeToDecimal(t) {
t = t.split(':');
return = t[0]*1 + '.' parseInt(t[1])/60;
}    

Am I missing something important? I just keep getting syntax errors.

I have a Javascript function that gives me the total hours worked in a day as HH:MM. I want to convert those numbers to decimals so I can add them up. I just seem to be getting errors at the moment:

function timeToDecimal(t) {
t = t.split(':');
return = t[0]*1 + '.' parseInt(t[1])/60;
}    

Am I missing something important? I just keep getting syntax errors.

Share Improve this question asked Apr 2, 2014 at 18:45 Jason BJason B 3651 gold badge5 silver badges15 bronze badges 5
  • 5 Looks like you're missing a + before parseInt()? – Michelle Commented Apr 2, 2014 at 18:46
  • 2 That would turn it into a string. I think removing the period would help. – Pete Commented Apr 2, 2014 at 18:47
  • 1 Also why not parse both numbers as int's? Doesn't make sense to only do one. – xd6_ Commented Apr 2, 2014 at 18:48
  • 1 Why add the '.'? When you divide minutes by 60, you'll get an answer in tenths. 30/60 = .5 – CM Kanode Commented Apr 2, 2014 at 18:51
  • What are the syntax errors? – Code-Apprentice Commented Jun 30, 2018 at 8:24
Add a comment  | 

3 Answers 3

Reset to default 26
function timeToDecimal(t) {
    var arr = t.split(':');
    var dec = parseInt((arr[1]/6)*10, 10);

    return parseFloat(parseInt(arr[0], 10) + '.' + (dec<10?'0':'') + dec);
}   

FIDDLE

returns a number with two decimals at the most

timeToDecimal('00:01') // 0.01
timeToDecimal('00:03') // 0.05
timeToDecimal('00:30') // 0.5
timeToDecimal('10:10') // 10.16
timeToDecimal('01:30') // 1.5
timeToDecimal('3:22' ) // 3.36
timeToDecimal('22:45') // 22.75
timeToDecimal('02:00') // 2

Some minor syntax errors as stated above:

Remove the '=', '.' and add parseInt

function timeToDecimal(t) {
  t = t.split(':');
  return parseInt(t[0], 10)*1 + parseInt(t[1], 10)/60;
}  

For the sake of completeness, here's a functional solution that would allow you to add more levels of accuracy: jsfiddle

function timeToDecimal(t) {
  return t.split(':')
          .map(function(val) { return parseInt(val, 10); } )
          .reduce( function(previousValue, currentValue, index, array){
              return previousValue + currentValue / Math.pow(60, index);
          });
};

console.log(timeToDecimal('2:49:50'));

You shouldn't put an = after the return, and let the Math generate the decimal point for you.

This should do it

function timeToDecimal(t) {
    t = t.split(':');
    return parseFloat(parseInt(t[0], 10) + parseInt(t[1], 10)/60);
}  

console.log(timeToDecimal('04:30')); // 4.5
console.log(timeToDecimal('04:22')); // 4.366666666666666
console.log(timeToDecimal('12:05')); // 12.083333333333334

Depending on what kind of calculations you want to do, you might want to round the result of course.

Here's the Fiddle

发布评论

评论列表(0)

  1. 暂无评论