Archive
TimeSince
Since I couldn't find it anywhere, here is a JavaScript version of time_since().
In case you have no clue about all this, Time Since is the time since a certain date, nicely put in a human readable string. Check out the demo !
Using Time Since is very easy, first you create a new date:
var myBirthday = new Date(2003, 10, 24, 6, 30);
Then, you get the time since this date:
alert(myBirthday.toTimeSinceString())
As you can see toTimeSinceString() is an instance method of Date. It takes three optional arguments: date.toTimeSinceString(number nLimit, string sBetween, string sLastBetween)
- nLimit
- limit the shown time units (year, month etc). default = 2
- sBetween
- string between two time units. default = ", "
- sLastBetween
- string between the second-last and last time unit. default = " and "
You can download the script by following the next link: this one!
Comments
Leave a Comment
Due to some recent spam, and because this site is no longer active I've decided to disable comments side-wide. You can always mail me if you have questions. Thanks!
Karl Bedingfield wrote:
Hi there.
Great script but I seem to be a having a problem with the dates.
I wanted to show my age so I followed your example with my DOB:
But the date is out by a month. It prints ‘It’s been 40 years and 8 months since my date of birth!’
Any ideas what might be causing this?
Thanks
Karl
February 9, 2004 @ 10:48 am. Type: Comment. Permalink.
Karl Bedingfield wrote:
Sorry I forgot the code tags:
February 9, 2004 @ 10:49 am. Type: Comment. Permalink.
Mark wrote:
Hi Karl,
this behaviour is because the TimeSince method doesn’t round values. The default is to show the date two “levels” deep. After the 40 years and 8 months there are 3 weeks and 4 days left. These aren’t rounded up, thus it shows 8 months.
I’m not sure if TimeSince is supposed to do the rounding, though. What do you think?
February 9, 2004 @ 8:14 pm. Type: Comment. Permalink.
Karl Bedingfield wrote:
Hi Mark,
Ah you have a point there.
I think you are correct when you say it should not be rounded up for older dates.
Thanks
Karl
February 11, 2004 @ 4:16 pm. Type: Comment. Permalink.