Skip to content

Instantly share code, notes, and snippets.

@bennadel
Created March 25, 2014 00:57
Show Gist options
  • Save bennadel/9753204 to your computer and use it in GitHub Desktop.
Save bennadel/9753204 to your computer and use it in GitHub Desktop.
Ask Ben: Displaying And Formatting The Difference Between Two Dates
<!--- 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment