Ask Ben: Displaying And Formatting The Difference Between Two Dates

Posted August 13, 2008 at 9:04 AM

Tags: ColdFusion, Ask Ben

Sir, i want to know the solution for which iam struggling for some days... that is how we have to caluculate difference between two dates such that it have to show the difference in the format yyyy (years):m (months): ww (weaks):w (weakdays) : h (hours): n (minutes) : s (seconds). Sir, to confirm exactly what my doubt mean is...if we go for ebay ..and if we find there the products displaying and the estimated time left to buy... there they show the time left...like that i want if i gve two dates it have to show the exact difference between them......as shown in above format..

thanking you sir.....for providing the oppurtunity and iam waiting for your reply kindly.......

This task is actually much less complicated than it might sound. ColdFusion is quite excellent at both handling and formatting dates. To solve this problem, at least mostly, all we need to is the following:

  • Find the difference between your two dates (the current time and the target time).
  • Format that difference in the desired way.

As you may have read before, ColdFusion's date math makes finding the difference between two dates simple - all we need to do is treat the dates as numbers and do some subtraction:

 Launch code in new window » Download code as text file »

  • <!--- Get a start date for our comparison. --->
  • <cfset dtFrom = ParseDateTime( "01/01/2008 12:00:00 AM" ) />
  •  
  • <!--- Get the current date. --->
  • <cfset dtTo = Now() />
  •  
  • <!---
  • Get the difference between these two dates. Using
  • ColdFusion Date Math will give us the numeric version
  • of the timespan that represents the difference between
  • these two dates... this timespan is represented in the
  • number of days between the two dates.
  • --->
  • <cfset dtDiff = (dtTo - dtFrom) />
  •  
  •  
  • <!---
  • Now that we have the difference between the two dates, we
  • simply need to format that date/time span using ColdFusion's
  • built-in formatting functions.
  • --->
  • #DateDiff( "yyyy", "12/30/1899", dtDiff )# Years,
  • #DateFormat( dtDiff, "m" )# Months,
  • #DateFormat( dtDiff, "d" )# Days,
  • #TimeFormat( dtDiff, "h" )# Hours,
  • #TimeFormat( dtDiff, "m" )# Minutes,
  • #TimeFormat( dtDiff, "s" )# Seconds

When outputting the difference, you have to be careful of the year. That is one tricky thing in ColdFusion. Dates don't start at zero - they start at 12/30/1899. Therefore, when we get our time span, we have to get the difference in years between our time span value and the "start" date of ColdFusion dates. The rest of the formatting should be straightforward.

Running the above code, we get the following output:

0 Years, 8 Months, 12 Days, 9 Hours, 2 Minutes, 42 Seconds

I hope this puts you on the right track.

Download Code Snippet ZIP File

Post Comment  |  Ask Ben  |  Permalink  |  Other Searches  |  Print Page




Learning ColdFusion 9 - ColdFusion 9 tutorials, samples, examples, demos

Reader Comments

Aug 15, 2008 at 3:01 PM // reply »
55 Comments

Ben,

I couldn't resist taking this a little further http://www.stephenwithington.com/blog/index.cfm/2008/8/15/Using-ColdFusion-to-Create-an-Ebayesque-Auction-Countdown-Timer-Custom-Tag


Aug 15, 2008 at 3:06 PM // reply »
6,516 Comments

@Steve,

That's some slick stuff.


Jun 26, 2009 at 2:20 PM // reply »
2 Comments

When I use this functionality for "d", "h" and "m" on a date from an MSSQL database (should it matter?)

<cfset dtFrom = sel_last_call.eventdate/>
<cfset dtTo = Now() />
<cfset dtDiff = (dtTo - dtFrom) />
#DateFormat( dtDiff, "d" )# Day<cfif DateFormat( dtDiff, "d" ) neq 1>s</cfif>,
#TimeFormat( dtDiff, "h" )# Hour<cfif TimeFormat( dtDiff, "h" ) neq 1>s</cfif>,
#TimeFormat( dtDiff, "m" )# Minute<cfif TimeFormat( dtDiff, "m" ) neq 1>s</cfif>

I get some odd results

The difference between
6/23/2009 4:40:00 PM and Now (6/26/09 2:15 pm)
1 Day 9 Hours 35 Minutes

6/24/2009 12:00:00 PM and Now (6/26/09 2:15 pm)
1 Day 2 Hours 15 Minutes


Jun 26, 2009 at 5:37 PM // reply »
6,516 Comments

@Charles,

First off, where the date comes from (MS SQL, MySQL, ACCESS, etc.) is not relevant. The issue you are having is not apparent right away - definitely not intuitive unless you work with date math a lot.

See, when you subtract one date from another using mathematical operators, you create a numeric date value. In the first example you gave, FROM - TO, results in:

2.89930555555

This value is a numeric representation of the time span between your two dates.

Now, the way we numeric dates work is that they are considered the number of days since the ZERO date in ColdFusion, which you can see if you do this:

#DateFormat( 0, "full" )#

... which gives us:

Saturday, December 30, 1899

And so, your mathematical date value, 2.8993...., is consider to be 2.8993.... days AFTER 12-30-1899.

So then, the next thing you are asking it is to get the date of that date:

DateFormat( dtDiff, "d" )

This is giving you "1". This is because if you add 2.899 days to 12-30-1899, you get 1-1-1900. And, the "day" of that is "1".

What you want is NOT the DateFormat() of the time span, but rather the DAYS of the time span, which is:

#Fix( dtDiff )#

Fix() in ColdFusion truncates a number (removes decimal), leaving you with just the integer, which is the number of dates in the time span.

The next issue is that we are dealing with 24 hours, but you are using the small "h", which is 12 hours. So, it gives you 9 hours, but really, its thinking 24 hours time, so its 21.

All together, you need to do this:

<cfset dtFrom = ParseDateTime( "6/23/2009 4:40:00 PM" ) />
<cfset dtTo = ParseDateTime( "6/26/2009 2:15:00 PM" ) />
<cfset dtDiff = (dtTo - dtFrom) />

#Fix( dtDiff )# day(s)
#TimeFormat( dtDiff, "H" )# hour(s)
#TimeFormat( dtDiff, "m" )# miunutes(s)

... which results in :

2 day(s) 21 hour(s) 35 miunutes(s)

I hope that helps clear it up a bit.


Jun 29, 2009 at 7:51 AM // reply »
2 Comments

Ahh... got it. Thanks mucho.


Post Comment  |  Ask Ben

Recent Blog Comments
Nov 20, 2009 at 11:32 PM
Five Months Without Hungarian Notation And I'm Loving It
I've used headless camel case for years for not only ColdFusion variables, but also SQL tables and fields... pretty much everything involving code. I also subscribe to the "don't abbreviate and clea ... read »
Nov 20, 2009 at 11:00 PM
Five Months Without Hungarian Notation And I'm Loving It
@Marcel, Yeah, I always err on the side of longer but more readable variable names. As for the camel casing of CF methods and the headless camel casing of custom items, I get around this by always ... read »
Nov 20, 2009 at 10:56 PM
Five Months Without Hungarian Notation And I'm Loving It
I use the following and love it: my.namespace.MyComponents.functionMethodsOrUDF() CONSTANT_VALUES_OR_PROPERTIES One thing I always try is to CamelCaseBuiltInColdFusionFunctions() so others can tell ... read »
Nov 20, 2009 at 5:38 PM
Learning ColdFusion 8: CFImage Part I - Reading And Writing Images
Hi Ben, Great article. I've been looking around to see if ColdFusion image engine can programatically create the following "wrap around" effect: http://www.creativepro.com/article/photoshop-s-she ... read »
Nov 20, 2009 at 5:35 PM
Maintaining ColdFusion Sessions Across SMS Text Message Requests Without Cookies
@Dave: I talked to Gert he suggested: <cfhttp method="get" url="http://{some cf website}" result="stuff" addtoken="yes" /> Note the addition of cfhttp attribute addtoken. That should persist y ... read »
Nov 20, 2009 at 5:23 PM
Maintaining ColdFusion Sessions Across SMS Text Message Requests Without Cookies
@Todd, Ahh, gotcha, yeah that makes sense. ... read »
Nov 20, 2009 at 5:17 PM
Maintaining ColdFusion Sessions Across SMS Text Message Requests Without Cookies
Ben, sorry if I didn't make this clear. You can make it work like that if you want, just put <cfset session.foo = 1> (and <cfset application.foo = 1>) in your OnRequestStart() and it reve ... read »