I want to take two times (in seconds since epoch) and show the difference between the two in formats like:
- 2 minutes
- 1 hour, 15 minutes
- 3 hours, 9 minutes
- 1 minute ago
- 1 hour, 2 minutes ago
How can I accomplish this??
I want to take two times (in seconds since epoch) and show the difference between the two in formats like:
How can I accomplish this??
I think you are looking for something like PrettyTime
PrettyTime is an OpenSource time formatting library. Completely customizable, it creates human readable, relative timestamps like those seen on Digg, Twitter, and Facebook.Its available in over 30 languages!
link: https://github.com/ocpsoft/prettytime
Can be used in android too
For example
If your time-spans cross daylight-saving (summer-time) boundaries, do you want to report the number of days?
For example, 23:00 to 23:00 the next day is always a day but may be 23, 24 or 25 hours depending on whether the you cross a daylight-savings transition.
If you care about that, make sure you factor it into your choice.
Yup awakened the dead I have, but here's my improved implementation based on @mtim posted code, as this thread comes almost on top of the searches so I am messing with the sleepy hollow,
It obviously can be improved. basically it tries to get the time span more friendly, there are a few limitation though i.e. it would behave strangely if the time passed (parameter) is in future, and its limited up to the days, hours and seconds only (months and years not handled, so that someone else can ;-).
sample outputs are:
, cheers :D
EDIT: now supports months and years :P
Recently I got the same requirement where I need a basic method to print duration. I was not having an option to use any third party library. So I just refined the idea of @mtim further.
The output will be time gap (duration) printed in words e.g.
1 day 2 hours 51 mins 41 secs ago
some code that plays with date formatting... and time ago.