23

How can I convert a decimal hour value like 1.6578 to hh:mm:ss in jquery or javascript?

I only managed to do it to hh:mm using this code:

var decimaltime= "1.6578";
var hrs = parseInt(Number(decimaltime));
var min = Math.round((Number(decimaltime)-hrs) * 60);
var clocktime = hrs+':'+min;
Stevoisiak
  • 23,794
  • 27
  • 122
  • 225
  • 2
    [moment().startOf('day').add(parseFloat("1.6578"), "hours").format("hh:mm:ss");](http://momentjs.com/) – Neil Feb 17 '16 at 15:14
  • Thanks Neil, I would like to learn how to do it in pure javascript if possible and would also like to avoid adding more libraries to my page to cut down on my already bloated loading time. Moment.js looks awesome though! –  Feb 17 '16 at 15:17

5 Answers5

29

Rather than doing the calculations yourself, use built-in functionality to set the seconds on an arbitrary date of a Date object, convert it to a string, and chop off the date part, leaving just the hh:mm:ss string.

var decimalTimeString = "1.6578";
var n = new Date(0,0);
n.setSeconds(+decimalTimeString * 60 * 60);
document.write(n.toTimeString().slice(0, 8));
Andrew Willems
  • 11,880
  • 10
  • 53
  • 70
  • 5
    If you only want hours and minutes (hh:mm), you can use `n.setMinutes(+decimalTimeString * 60); var result = n.toTimeString().slice(0, 5);` – Stevoisiak Jul 16 '18 at 17:33
13

You could do something like this:

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);
brso05
  • 13,142
  • 2
  • 21
  • 40
3

I found this function worked very well in converting my decimal value of seconds into a standard time string of hh:mm:ss

function formatDuration(seconds) {
  return new Date(seconds * 1000).toISOString().substring(11, 11 + 8);
}
const secondsInDecimal = 1.6578;
formatDuration(secondsInDecimal);

// "00:00:01"

References:

Process:

  1. new Date(seconds * 1000) returns this string "Wed Dec 31 1969 19:00:01 GMT-0500 (Eastern Standard Time)"
  2. toISOString() is a method on the Date object that changes the returned string into, "1970-01-01T00:00:01.657Z". This is a simplified string returned from #1 above.
  3. Running substring() on that string returns a portion of it, from the 11th index, out for a length of 8 more indexes (indices). That value is, "00:00:01".
Dani Amsalem
  • 1,266
  • 17
  • 26
2

I realize this is not exactly what the original poster needed, but I needed to get days, hours, and minutes from a decimal time that I could then use for calculations and setting a date object. Many other posts are using outdated methods involving strings and slicing, etc.

const decimalHours = 27.33;
const n = new Date(0,0);
n.setMinutes(+Math.round(decimalHours * 60)); 
const days = (n.getDate() - 1)
const hours = n.getHours()
const minutes = n.getMinutes()
console.log("Days: ",days, "Hours: ",hours, "Minutes: ",minutes)

const decimalHours = 4.33;
const n = new Date(0,0);
n.setMinutes(+Math.round(decimalHours * 60)); 
const days = (n.getDate() - 1)
const hours = n.getHours()
const minutes = n.getMinutes()
console.log("Days: ",days, "Hours: ",hours, "Minutes: ",minutes)

Note that this is not designed for hour values over a month long.

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
Ben in CA
  • 688
  • 8
  • 22
0

This is working for my purpose

function formatHoursMinutesSeconds(num){
  var hours = Math.floor(num * 24);
  var minutes = Math.floor(((num * 24) - hours) * 60);
  var seconds = Math.floor(((((num * 24) - hours) * 60)-minutes)*60);

  return (hours + ":" + minutes.toString().padStart(2, '0') + ":" + 
  seconds.toString().padStart(2, '0'));
 }