Display relative time in hour, day, month and year

Posted by JohnJohnGa on Stack Overflow See other posts from Stack Overflow or by JohnJohnGa
Published on 2012-08-31T15:36:43Z Indexed on 2012/08/31 15:38 UTC
Read the original article Hit count: 236

Filed under:
|

I wrote a function

toBeautyString(epoch) : String

which given a epoch, return a string which will display the relative time from now in hour and minute

For instance:

// epoch: 1346140800 -> Tue, 28 Aug 2012 05:00:00 GMT 
// and now: 1346313600 -> Thu, 30 Aug 2012 08:00:00 GMT
toBeautyString(1346140800) 
-> "2 days and 3 hours ago"

I want now to extend this function to month and year, so it will be able to print:

2 years, 1 month, 3 days and 1 hour ago

Only with epoch without any external libraries. The purpose of this function is to give to the user a better way to visualize the time in the past.

I found this: Calculating relative time but the granularity is not enough.

© Stack Overflow or respective owner

Related posts about algorithm

Related posts about time