公式计算年数/月/日...从长(毫秒) [英] Formula to calculate number of years/months/days... from long (milliseconds)

查看:269
本文介绍了公式计算年数/月/日...从长(毫秒)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a certain duration value (endDate - startDate = duration in milliseconds), i need to get the time difference in such format:

0年5个月3天...

0 years 5 months 3 days ...

我需要数学公式,可以帮助我从那段时间内提取年/月/日/小时/分钟/秒的数量。

I need the mathematical formulas that can help me extract the number of years/months/days/hours/mins/secs from that duration.

我使用的是XSLT 1.0,不能使用任何XPath2.0日期函数。所以我需要自己写这个函数。

I am using XSLT 1.0 and can't use any XPath2.0 date functions. so i need to write the function myself.

推荐答案

下降到几天很简单

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

这样写的方式可以重复分配给同一个变量,但是您也可以在单个步骤中进行配置:

this is written in a way that you repeatedly assign to the same variable, but you can also formulate this in a single step each:

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天,但这会有些不准确。

If you want to go to months, you have to deal with different lengths of months. Same for years. You could assume a year of 365 days, but that would be somewhat inaccurate.

这篇关于公式计算年数/月/日...从长(毫秒)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆