How can I compare/format 2 times in coldfusion?

I am trying to subtract 1 date/time from another and I need to display the time difference in format: 0:00:00:00 that is days:hours:minutes:seconds.

I am using something like the below, but it is formatted like this: 0:0:7:28, I need it formatted like this: 00:00:07:28

If the below method isn't the best way to compare 2 times and display in the format I'm looking for, please recommend another method, thank you.

Thank you.

<cfoutput query="qTheQuery">
<cfset dtFrom = #qTheQuery.Created# />
<cfset dtTo =  #qTheQuery.Completed# />
<cfset sec=DateDiff("s",dtFrom,dtTo)>
<cfset days=int(sec/86400)>
<cfset hours=int((sec-(days*86400))/3600)>
<cfset minutes=int((sec-(days*86400)-(hours*3600))/60)>
<cfset seconds=(sec-(days*86400)-(hours*3600)-(minutes*60))>

Open in new window

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Use NumberFormat() to pad the values with zeroes:

<cfset days= numberFormat(  days, "00") >
<cfset hours=numberFormat( hours, "00") >

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
BTW, if you're going to use the logic in multiple places you might want to package it into a function.  There's an existing one called CreateTimeStruct function at cflib. It'll return the same result result, and you can customize it to zero pad (or not) on demand.

<!--- zero pads by default --->
<cfset result = CreateTimeStruct(sec, "s")/>
<cfdump var="#result#">

<!--- turn off zero padding --->
<cfset result = CreateTimeStruct(sec, "s", false)/>
<cfdump var="#result#">

Open in new window

original udf (with small tweak)

Edit:  Fix missing VAR scope for "key" variable

 * Converts a given number of days, hours, minutes, OR seconds to a struct of days, hours, minutes, AND seconds.
 * @param timespan 	 The timespan to convert. 
 * @return Returns a structure. 
 * @author Dave Pomerance ( 
 * @version 1, January 7, 2002 
function CreateTimeStruct(timespan) {
    var timestruct = StructNew();
    var mask = "s";
    var zeroPad = true;
    var key = "";

    if (ArrayLen(Arguments) gte 2) mask = Arguments[2];
    if (ArrayLen(Arguments) gte 3) zeroPad = Arguments[3];

	// if timespan isn't an integer, convert mask towards s until timespan is an integer or mask is s
	while (Int(timespan) neq timespan AND mask neq "s") {
		if (mask eq "d") {
			timespan = timespan * 24;
			mask = "h";
		} else if (mask eq "h") {
			timespan = timespan * 60;
			mask = "m";
		} else if (mask eq "m") {
			timespan = timespan * 60;
			mask = "s";
	// only 4 allowed values for mask - if not one of those, return blank struct
	if (ListFind("d,h,m,s", mask)) {
		// compute seconds
		if (mask eq "s") {
			timestruct.s = (timespan mod 60) + (timespan - Int(timespan));
			timespan = int(timespan/60);
			mask = "m";
		} else timestruct.s = 0;
		// compute minutes
		if (mask eq "m") {
			timestruct.m = timespan mod 60;
			timespan = int(timespan/60);
			mask = "h";
		} else timestruct.m = 0;
		// compute hours, days
		if (mask eq "h") {
			timestruct.h = timespan mod 24;
			timestruct.d = int(timespan/24);
		} else {
			timestruct.h = 0;
			timestruct.d = timespan;
        if (zeroPad) {
           for (key in timestruct) {
               timestruct[key] = numberFormat(timestruct[key], "00");
	return timestruct;

Open in new window

earwig75Author Commented:
You're the best, thank you.
Welcome :)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ColdFusion Language

From novice to tech pro — start learning today.