0

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.

ralph
  • 67
  • 7
  • Ok, so: what have you tried? How are you hoping to define "a month"? (e.g. what date is 1 month after January 30th?) – nickgrim Sep 30 '13 at 15:07
  • I see.. well we can skip the month – ralph Sep 30 '13 at 15:43
  • [http://stackoverflow.com/questions/8211744/convert-milliseconds-or-seconds-into-human-readable-form][1] provides an answer without calculating the month [1]: http://stackoverflow.com/questions/8211744/convert-milliseconds-or-seconds-into-human-readable-form – ralph Sep 30 '13 at 15:45

1 Answers1

1

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.

MvG
  • 57,380
  • 22
  • 148
  • 276