I'm having a problem with Coldfusion's DateDiff(). I'm trying to get the difference between two dates with times, like the following examples:
fromdate=06/11/2017 22:10 todate =16/11/2017 23:20 should return:
10 days, 1 hour and 10 minutes
fromdate=06/11/2017 22:10 todate =16/11/2017 20:20 should return:
9 days, 22 hours, 10 minutes
Any help?
Code:
<cfset dtFrom = "11/06/2017 22:10" /> <cfset dtTo = "11/16/2017 23:20" /> <cfoutput> #DateDiff( "d", dtFrom, dtTo)# Days, #DateDiff( "h", dtFrom, dtTo) % 24# Hours #DateDiff( "n", dtFrom, dtTo) % 24 % 60# Minutes </cfoutput> 32 Answers
In addition to the previous suggestion, DateDiff() isn't going to understand those specific strings or that "06/11/2017" should mean November 6. The result will be:
158 days 1 hours 10 minutes For it to work as expected, you must convert the strings into date objects first. For example use LSParseDateTime with the right Locale.
fromDate = lsParseDateTime("06/11/2017 22:10", "English (UK)", "dd/MM/yyyy hh:mm"); toDate = lsParseDateTime("16/11/2017 23:20", "English (UK)", "dd/MM/yyyy hh:mm"); or possibly:
fromDate = lsParseDateTime("06/11/2017 22:10", "English (UK)"); toDate = lsParseDateTime("16/11/2017 23:20", "English (UK)"); Here is one way.
totalMinutes = datediff("n", fromDate, toDate); days = int(totalMinutes /(24 * 60)) ; minutesRemaining = totalMinutes - (days * 24 * 60); hours = int(minutesRemaining / 60); minutes = minutesRemaining mod 60; writeoutput(days & ' days ' & hours & ' hours ' & minutes & ' minutes'); 0