Discussion:
Convert Decimal to hh:mm
(too old to reply)
JulianForniss
2005-03-08 23:19:15 UTC
Permalink
Hello and thank you in advance.

Is there a simple way to take a decimal value, say "5.5" and convert it to a time format "5:30"

Thanks,
Julian
AudiTT
2005-03-09 00:50:13 UTC
Permalink
Hi Julian, This is by no means perfect but it should get you started. Hope
that helps. Regards.

<cfscript>
function decimalToTime(decimal){
var timeMask = "hh:mm tt";
var timeValue = "";
var decimalMinutes = "";
var decimalHours = "";

//make sure passed value is numeric
if(not isNumeric(decimal)){
return "The value passed to function decimalToTime() is not a valid number!";
}
else{
timeValue = NumberFormat(decimal,"99.99");
}

if(timeValue LT 0 OR timeValue GTE 24){
return "The value passed to function decimalToTime() is not within the
valid range of 0 - 23.99";
}
else{

//if the optional mask was passed use that otherwise default to "hh:mm tt"
if(arrayLen(arguments) gt 1){
timeMask = arguments[2];
}

decimalHours = listfirst(timeValue,".");
decimalMinutes = listLast(timeValue,".");

//attempt to determine minutes
if(decimalMinutes neq 0){
decimalMinutes = round(60*decimalMinutes/100);
}

timeValue = TimeFormat(decimalHours & ":" & decimalMinutes,timeMask);
return timeValue;
}
}


variables.thisNumber = "5.5";
writeoutput("Decimal: " & variables.thisNumber & "<br /><br />Converted to
time: " & decimalToTime(variables.thisNumber));

</cfscript>
JulianForniss
2005-03-11 17:44:45 UTC
Permalink
Thank you very much for your effort. I'm sorry I didn't aclnowledge it sooner,
but for some reason, I was not alerted by email that you had replied.

This solution appears a good one. I was really hoping that there would be
some cfconverttotime tag!
dempster
2005-03-11 18:29:36 UTC
Permalink
If all you want to do is convert the decimal time to an equivalent
hours:minutes format, it just takes some simple math and formatting. Use the
Int function to get the hour value and subtract this from the original to get
the decimal part. Multiply the decimal part by 60, format to pad with 0 if it's
less than 10, and you're done. For example, if your original decimal time is
in a variable called dectime, this would display hours:minutes-
#Int(dectime)#:#NumberFormat((dectime - Int(dectime)) * 60,'00')#
Continue reading on narkive:
Loading...