Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
215 views
in Technique[技术] by (71.8m points)

xml - Formula to calculate number of years/months/days... from long (milliseconds)

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

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.

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Going down to days is simple.

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)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...