Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...