Coldfusion 计算天、小时、分钟中的秒数

发布于 2025-01-12 16:29:46 字数 601 浏览 1 评论 0原文

我想将秒转换为天、小时和分钟 目前,它只能工作几小时和几分钟,但不能工作几天。您能否支持我,告诉我我做错了什么:

<cfscript>
    seconds = '87400';
    midnight = CreateTime(0,0,0);
    time = DateAdd("s", seconds, variables.midnight);
    date= xxxxxxxxxxxxxxxxxxxxx???
</cfscript>

<cfoutput>
    #DateFormat(variables.date, 'd')#  not working 
    #TimeFormat(variables.time, 'HH:mm')#
</cfoutput>

对于值 87400 ,预期结果是

  • 1 天,0 小时,16 分钟

如果我花费 94152 秒,它将是:

  • 1天,3小时,22分钟

我唯一的问题是获得正确的日期...显示了小时和分钟,但不是正确的日期,

谢谢大家的支持

I want to convert seconds to days, hours and minutes
Currently, it works just for hours and minutes but not for days. Can you please support me tell me what I did wrong:

<cfscript>
    seconds = '87400';
    midnight = CreateTime(0,0,0);
    time = DateAdd("s", seconds, variables.midnight);
    date= xxxxxxxxxxxxxxxxxxxxx???
</cfscript>

<cfoutput>
    #DateFormat(variables.date, 'd')#  not working 
    #TimeFormat(variables.time, 'HH:mm')#
</cfoutput>

For the value 87400 the expected result is

  • 1 Days, 0 hours, 16 minutes

If I take 94152 seconds it will be:

  • 1 days, 3 hours, 22 minutes

The only issue i have is to get the correct days ... hours and minutes are diplayed but not the correct days

thank you for all the support

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

晚雾 2025-01-19 16:29:46

计算间隔的一种简单方法是利用 模数运算符

totalSeconds  = 94152;
days = int(totalSeconds / 86400); 
hours = totalSeconds / 3600 % 24; 
minutes = totalSeconds / 60 % 60; 
seconds = totalSeconds % 60;

对于 94152 秒,结果将为:

间隔
DAYS1
HOURS2
分钟9
12
总秒94152

演示 trycf.com

A simple way to calculate the intervals is by taking advantage of the modulus operator:

totalSeconds  = 94152;
days = int(totalSeconds / 86400); 
hours = totalSeconds / 3600 % 24; 
minutes = totalSeconds / 60 % 60; 
seconds = totalSeconds % 60;

For 94152 seconds, the results would be:

IntervalValue
DAYS1
HOURS2
MINUTES9
SECONDS12
TOTALSECONDS94152

demo trycf.com

鹿港巷口少年归 2025-01-19 16:29:46

我从你的问题中了解到,你不需要沿着时间线获取特定的日期和时间,而是需要将总秒数转换为天、小时和分钟。为此,您不必使用 cfml 时间和日期函数,如 CreateTime() 或 DateAdd()。您可能只需要这些来获取时间轴上的时间或日期参考点,但情况似乎并非如此,否则您会知道起始日期变量的值。因此,你可以用简单的三法则来解决这个问题。可能有更简单的方法,所以我只发布一个替代方法。

我们知道:

    60 seconds is equivalent to 1 minute
    60 minutes is equivalent to 1 hour
    24 hours is equivalent to 1 day

因此,您在 cfml 中的计算可能如下所示:

<cfscript>
    //Constants for calculation
    secondsPerDay= 60*60*24;
    secondsPerHour= 60*60;
    secondsPerMinute= 60;

    //Seconds to convert
    secondsTotal=87400;
     
    // temp variable
    secondsRemain= secondsTotal;
    
    days= int( secondsRemain / secondsPerDay);
    secondsRemain= secondsRemain - days * secondsPerDay;
    hours= int( secondsRemain / secondsPerHour);
    secondsRemain= secondsRemain - hours * secondsPerHour;
    minutes= int( secondsRemain / secondsPerMinute);
    secondsRemain= secondsRemain - minutes * secondsPerMinute;

    writeoutput( "#secondsTotal# seconds are: #days# days, #hours# hours, #minutes# minutes and #secondsRemain# seconds." );

</cfscript>

输出:

87400 seconds are: 1 days, 0 hours, 16 minutes and 40 seconds.

I understand from your question that you don't need to get a certain date and time along a timeline, but convert a total amount of seconds in days, hours and minutes. To do that you don't necessary need to use cfml time and date functions like CreateTime() or DateAdd(). You just may need these in order to get a reference point of time or date along a timeline, which doesn't seem to be the case, otherwise you would know the value of your starting date variable. Thus, you can solve this with plain rule of three. There may be simpler methods, so I'm posting an alternative only.

We know that:

    60 seconds is equivalent to 1 minute
    60 minutes is equivalent to 1 hour
    24 hours is equivalent to 1 day

Thus, your calcualtion within cfml could be like so:

<cfscript>
    //Constants for calculation
    secondsPerDay= 60*60*24;
    secondsPerHour= 60*60;
    secondsPerMinute= 60;

    //Seconds to convert
    secondsTotal=87400;
     
    // temp variable
    secondsRemain= secondsTotal;
    
    days= int( secondsRemain / secondsPerDay);
    secondsRemain= secondsRemain - days * secondsPerDay;
    hours= int( secondsRemain / secondsPerHour);
    secondsRemain= secondsRemain - hours * secondsPerHour;
    minutes= int( secondsRemain / secondsPerMinute);
    secondsRemain= secondsRemain - minutes * secondsPerMinute;

    writeoutput( "#secondsTotal# seconds are: #days# days, #hours# hours, #minutes# minutes and #secondsRemain# seconds." );

</cfscript>

That outputs:

87400 seconds are: 1 days, 0 hours, 16 minutes and 40 seconds.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文