【问题标题】:Coldfusion date difference in days and hours天和小时的冷融合日期差异
【发布时间】:2017-11-06 10:11:50
【问题描述】:

我遇到了 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>

【问题讨论】:

  • 你尝试过什么,它返回了什么?还要在代码块中添加源代码,这样我们就可以看到你做了什么
  • code &lt;cfset dtFrom = "11/06/2017 22:10" /&gt; &lt;cfset dtTo = "11/16/2017 23:20" /&gt; #DateDiff( "d", dtFrom, dtTo)# Days, #DateDiff( "h", dtFrom, dtTo) % 24# Hours #DateDiff( "n", dtFrom, dtTo) % 24 % 60# Minutes 返回:10 天,1 小时 22 分钟 code &lt;cfset dtFrom = "11/06/2017 22:10" /&gt; &lt;cfset dtTo = "11/16/2017 21:20" /&gt; 返回:9 天,23 小时 22 分钟
  • 您可以将其编辑到您的帖子中吗?它已经工作了吗?

标签: date coldfusion datediff


【解决方案1】:

这是一种方法。

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');

【讨论】:

    【解决方案2】:

    除了the previous suggestionDateDiff() 不会理解那些特定字符串或“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)");
    

    【讨论】:

      猜你喜欢
      • 2011-03-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-26
      • 1970-01-01
      • 2023-03-29
      • 1970-01-01
      相关资源
      最近更新 更多