开发者

How to get the number of years, months, days, hours and mins of a certain number of seconds?

Given an arbitrary number of seconds, how can I get the number of years, months, da开发者_开发百科ys, hours and mins?

The algorithm should first compute the maximum number of years, then the number of months and so on...

What is an efficient way to do this?


It's mostly down to plain division. As you may know...

  • A minute has 60 seconds:
    number_of_minutes := floor(number_of_seconds / 60)

  • An hour has 60 minutes:
    number_of_hours := floor(number_of_minutes / 60) or
    number_of_hours := floor(number_of_seconds / (60 * 60))

  • A day has 24 hours (at least usually... see below.)

  • A month has anything between 28 to 31 days.

  • A year has 365 or 366 days, or 365.2425 days on average.

The last two I mentioned may require you to think more about the stated problem. Either you define an "average" month, which then allows you to say "x seconds equal y average months"; or you don't convert your seconds to months at all.

(Thinking about it, if you were talking to an astronomer or alike, they would probably tell you that a day is not always exactly 24 hours, due to the occasional leap second.)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜