【问题标题】:Formula to calculate number of years/months/days... from long (milliseconds)计算年数/月数/天数的公式......从长(毫秒)
【发布时间】:2013-09-30 14:59:56
【问题描述】:

我有一定的持续时间值(endDate - startDate = 持续时间,以毫秒为单位),我需要以这种格式获取时间差:

0 年 5 个月 3 天 ...

我需要可以帮助我从该持续时间中提取年/月/日/小时/分钟/秒数的数学公式。

我正在使用 XSLT 1.0 并且不能使用任何 XPath2.0 日期函数。所以我需要自己编写函数。

【问题讨论】:

标签: xml datetime xslt math formula


【解决方案1】:

精确到几天很简单。

tmp = time in milliseconds
milliseconds = tmp mod 1000
tmp = floor(tmp / 1000)
seconds = tmp mod 60
tmp = floor(tmp / 60)
minutes = tmp mod 60
tmp = floor(tmp / 60)
hours = tmp mod 24
tmp = floor(tmp / 24)
days = tmp

这是以您重复分配给同一个变量的方式编写的,但您也可以在单个步骤中制定它:

milliseconds = time mod 1000
seconds = floor(tmp / 1000) mod 60
minutes = floor(tmp / 60000) mod 60
hours = floor(tmp / 3600000) mod 24
days = floor(tmp / 86400000)

如果你想去几个月,你必须处理不同长度的月份。多年来一样。您可以假设一年有 365 天,但这有点不准确。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-01
    • 2021-12-25
    • 2013-03-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多