Fresheyeball
Fresheyeball

Reputation: 30015

Datejs getting the difference between dates

So here is my goal, I want to get how long ago a certain date was as a pretty readable string. Something like: '4 day and 3 hours ago'. I am using datejs and have both my dates currently as epochs.

I know I could subtract one epoch from the other, take the difference and then manually calculate the string to output. But this feels like something datejs would handle. However I see no documentation (of any js lib for that matter) expressing a means of converting a date object, or an epoch to a quantity of time.

any ideas, on how this best should be handled?

UPDATE

Let me be more clear. When I say human readable that means

What I ended up doing

    prettyAgo = (ago) ->
        ago = ago.getTime() if typeof ago is 'object'
        diff = ((new Date()).getTime() - ago) / 1000
        dayDiff = Math.floor(diff / 86400)
        return false if isNaN(dayDiff) or dayDiff < 0

        writer = ''

        if dayDiff is 0
            if diff < 1 then writer             = 'just now'
            else if diff < 60 then writer       = Math.floor(diff)+' seconds ago'
            else if diff < 120 then writer      = '1 minute ago'
            else if diff < 3600 then writer     = Math.floor(diff / 60)+' minutes ago'
            else if diff < 7200 then writer     = '1 hour ago'
            else if diff < 86400 then writer    = Math.floor(diff / 3600)+' hours ago'
        else if dayDiff is 1 then writer        = 'yesterday'
        else if dayDiff < 7 then writer         = dayDiff+' days ago'
        else if dayDiff is 7 then writer        = '1 week ago'
        else if dayDiff < 31 then writer        = Math.ceil( dayDiff / 7 )+' weeks ago'
        else writer                             = new Date(ago).toString 'MMM yyyy'

        return writer

Upvotes: 1

Views: 3184

Answers (2)

Ed Bayiates
Ed Bayiates

Reputation: 11210

DateJS does handle this by including a class called TimeSpan (defined in time.js, not in the base JS file) which does the calculations for you. It doesn't do the formatting, so you'd need something like this:

function showDiff(date1, date2) {
    var diff, rv, yrs;

    diff = new TimeSpan(date2 - date1);
    if (diff.days > 0) {
        // A day or more difference
        if (diff.days === 1) {
            rv = "yesterday";
        }
        else if (diff.days >= 365) {
            yrs = diff.days / 365;
            if (yrs === 1) {
                rv = "1 year ago";
            }
            else {
                rv = yrs + " years ago";
            }
        }
        else {
            rv = diff.days + " ago";
        }
    }
    else {
        // Less than 1 day difference
        if (diff.hours > 0) {
            if (diff.hours === 1) {
                rv = "1 hour ago";
            }
            else {
                rv = diff.hours + " hours ago";
            }
        }
        else if (diff.minutes > 0) {
            if (diff.minutes === 1) {
                rv = "1 minute ago";
            }
            else {
                rv = diff.minutes + " minutes ago";
            }
        }
        else {
            rv = "just now";
        }

        return rv;
    }

Obviously this isn't perfect and doesn't handle leap years and all that, but it should give you the idea.

Upvotes: 2

Derek 朕會功夫
Derek 朕會功夫

Reputation: 94299

Actually you can do that in pure JavaScript.

function format(num) {
    var date = new Date(num),
        time = [];
    with(time) {
        push(date.getUTCDate() - 1);
        push(date.getUTCHours());
        push(date.getUTCMinutes());
        push(date.getUTCSeconds());
    }
    if (time[0] > 29) {
        time[0] = Math.floor(date / 86400000);
    }
    return time[0] + " day " + time[1] + " hours " + time[2] + " mintues " + time[3] + " seconds ago";
}

http://jsfiddle.net/DerekL/NqTnF/

Upvotes: 1

Related Questions