There can be no such algorithm!
The fractions of the day (hours, minutes, seconds), even days, are not a problem. The problem is that the length of the "month" varies from 28 to 31 days.
I will give you an example:
Suppose today is 28 Feb 2013 and you want to calculate toBeautyString(28 Jan 2013) :
today: 28 Feb 2013 toBeautyString(28 Jan 2013) expected answer: 1 month ago
Well, that is really not a problem. On the same day, in the same year, only the month changed.
Now calculate toBeautyString(27 Jan 2013) on the same day:
today: 28 Feb 2013 toBeautyString(27 Jan 2013) expected answer: 1 month and 1 day ago
It's easy too, isn't it? We wanted to get the value the day before, and the output indicated that the duration is one day longer.
Now let's go to bed and continue to work the next day (March 1, 2013).
Try the following:
today: 1 Mar 2013 toBeautyString(1 Feb 2013) expected answer: 1 month ago
Well, so simple! The same logic as your first calculation. Only the month has changed by 1, so the duration cannot be anything but 1 month.
So, let's calculate the value the day before:
today: 1 Mar 2013 toBeautyString(31 Jan 2013) expected answer: 1 month and 1 day ago
Again: the result of the previous day should be a duration that is 1 day longer.
Try making a duration of up to 1 day:
today: 1 Mar 2013 toBeautyString(30 Jan 2013) expected answer: 1 month and 2 days ago
and longer:
today: 1 Mar 2013 toBeautyString(29 Jan 2013) expected answer: 1 month and 3 days ago
and finally:
today: 1 Mar 2013 toBeautyString(28 Jan 2013) expected answer: 1 month and 4 days ago
Remember this!
No, we repeat the very first calculation that we did yesterday. Yesterday we counted toBeautyString(28 Jan 2013) and the result was 1 month ago . Today is the next day. If we calculate toBeautyString(28 Jan 2013) today, the result should show us a duration that is one day longer:
today: 1 Mar 2013 toBeautyString(28 Jan 2013) expected answer 1 month and 1 days ago
Compare this to the previous calculation. We did both calculations on March 1, 2013. And in both cases, we calculated the same thing: toBeautyString(28 Jan 2013) . But we expect two different results. Surprisingly, both expectations are true.
So, in order to deliver a result that truly meets our expectations, the algorithm must be able to read our mind. But this is not possible for the algorithm, therefore, there cannot be an algorithm that perfectly performs what you expect.