我遇到Coldfusion的DateDiff()问题。我试图通过时间来区分两个日期之间的区别,如下面的例子:
fromdate=06/11/2017 22:10
todate =16/11/2017 23:20
应该返回:
10天,1小时10分钟
fromdate=06/11/2017 22:10
todate =16/11/2017 20:20
应该返回:
9天,22小时,10分钟
任何帮助?
代码:
<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>
答案 0 :(得分:4)
除了the previous suggestion之外,DateDiff()
无法理解那些特定的字符串,或者“06/11/2017”应该是11月6日。结果将是是:
158 days 1 hours 10 minutes
要使其按预期工作,必须先将字符串转换为日期对象。例如,使用LSParseDateTime和正确的区域设置。
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");
或可能:
fromDate = lsParseDateTime("06/11/2017 22:10", "English (UK)");
toDate = lsParseDateTime("16/11/2017 23:20", "English (UK)");
答案 1 :(得分:2)
这是一种方式。
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');