From Decimal to 'hh:mm:ss' Conversion

LdR
Tera Expert

Hi,

Is it possible to convert a decimal field (representing hours) into the 'hh: ss' format?
Example:
1.5 h → 1h 30 minutes

Thank you.

1 ACCEPTED SOLUTION

ARG645
Tera Guru

Things to consider: 

input parameter cannot be less than Zero as you are converting the Decimal hh:mm:ss format.

decimalTohhmmss(1.5); //will give you 01:30:00
decimalTohhmmss(0.5); //will give you 00:30:00
decimalTohhmmss(-1.0); //Will give nothing

function decimalTohhmmss(decimal){
	if(decimal< 0) // return nothing if the parameter is negative
	return;
	var totalSeconds = decimal*3600;
	var hours   = Math.floor(totalSeconds / 3600);
	var minutes = Math.floor((totalSeconds - (hours * 3600)) / 60);
	var seconds = totalSeconds - (hours * 3600) - (minutes * 60);
			
	// round seconds
	seconds = Math.round(seconds * 100) / 100;
			
	var result = (hours < 10 ? "0" + hours : hours);
	result += ":" + (minutes < 10 ? "0" + minutes : minutes);
	result += ":" + (seconds  < 10 ? "0" + seconds : seconds);
	return result;
};

View solution in original post

5 REPLIES 5

lloques1
Kilo Contributor

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;
gs.print("" + hours + ":" + minutes + ":" + seconds);

 

From:

https://stackoverflow.com/questions/35460303/how-to-convert-decimal-hour-value-to-hhmmss 

Mark Stanger
Giga Sage

It is possible.  This function should do just what you need.  Just pass your decimal in as shown here.

var convertedTime = hrTohhmm(1.5);

gs.print(convertedTime);

function hrTohhmm(hours){
    var sign = hours < 0 ? "-" : "";
    var hour = Math.floor(Math.abs(hours));
    var min = Math.floor((Math.abs(hours) * 60) % 60);
    return sign + (hour < 10 ? "0" : "") + hour + " " + (hour > 1 ? "hours" : "hour") + " " + (min < 10 ? "0" : "") + min + " " + (min > 1 ? "minutes" : "minute");
}

Just checking on on this one.  Has this question been answered or is there more information I can help you with?  If it's been answered, please mark the answer above as the correct one so people know this has been taken care of.  Thanks!

ARG645
Tera Guru

Things to consider: 

input parameter cannot be less than Zero as you are converting the Decimal hh:mm:ss format.

decimalTohhmmss(1.5); //will give you 01:30:00
decimalTohhmmss(0.5); //will give you 00:30:00
decimalTohhmmss(-1.0); //Will give nothing

function decimalTohhmmss(decimal){
	if(decimal< 0) // return nothing if the parameter is negative
	return;
	var totalSeconds = decimal*3600;
	var hours   = Math.floor(totalSeconds / 3600);
	var minutes = Math.floor((totalSeconds - (hours * 3600)) / 60);
	var seconds = totalSeconds - (hours * 3600) - (minutes * 60);
			
	// round seconds
	seconds = Math.round(seconds * 100) / 100;
			
	var result = (hours < 10 ? "0" + hours : hours);
	result += ":" + (minutes < 10 ? "0" + minutes : minutes);
	result += ":" + (seconds  < 10 ? "0" + seconds : seconds);
	return result;
};