n

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

  1. 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.

  2. Karl Bedingfield wrote:

    Sorry I forgot the code tags:


    February 9, 2004 @ 10:49 am. Type: Comment. Permalink.

  3. 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.

  4. 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.

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!


Navigate


Colophon

Hey! I'd like you to know that this site will be gone soon. Problems with this site (which never was intented as a final release, but has been active for some months already now) will not be solved, sorry for any inconvenience.

The future address of the new site will be http://novemberborn.net/

Summary

Oops, there is no summary.

Context


Misc.

Copyright 2003 - 2004 Mark Wubben. This website is licensed under a Creative Commons License. Powered by WordPress