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:
<!--- 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.
Want to use code from this post? Check out the license.