Created
March 25, 2014 00:57
-
-
Save bennadel/9753204 to your computer and use it in GitHub Desktop.
Ask Ben: Displaying And Formatting The Difference Between Two Dates
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!--- 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