rmm5t / jquery-timeago

:clock8: The original jQuery plugin that makes it easy to support automatically updating fuzzy timestamps (e.g. "4 minutes ago").
http://timeago.yarp.com
MIT License
3.82k stars 710 forks source link

Specify what time to calculate difference from. #103

Open dillongrove opened 11 years ago

dillongrove commented 11 years ago

I noticed that this issue has been brought up before (see here), but I don't think a good justification was ever given, so I'd like to chime in with my own.

I'm writing a simple message/commenting system. Every time a user submits a comment, the comment is stored in the database along with the UTC time that it was posted. Then, when the comments are displayed, I output that time in the DOM, to be used by the timeago plugin.

The issue with this is the "current time" that the timeago plugin uses to compute the difference. It generates this current time by using javascript's new Date() function. This gives us the current time in the user's timezone. The problem with this is of course that the computed difference is only accurate if the user is in UTC time too.

So what I'd like to do is instead compute the difference using the current UTC time as the current time. As far as I can tell there's not a straightforward way to just get the current UTC time in javascript. Instead, we can use the getTimezoneOffset() function. I modified the distance function in the plugin as follows:

function distance(date) {
    var UTC_date_string = new Date().toUTCString(); // get current UTC Time in string form
    var millis_since_epoch = Date.parse(UTC_date_string); // convert it to milliseconds

    /* The date passed in comes to us in the client's timezone because it was created using the javascript
     * new Date() constructor when it was parsed form the DOM.  Unforunately, the date in the DOM is from
     * the server, and already in UTC.  We can account for this discrepancy by subtracting the client's 
     * timezone offset.
     */
    var date_millis = date.getTime(); // convert passed in date to milliseconds (still wrong timezone)
    var minutes_off = date.getTimezoneOffset(); // get the number of minutes this timezone differs from UTC time
    var millis_off = minutes_off*60*1000; // convert from minutes to milliseconds
    var date_millis_since_epoch = date_millis - millis_off; // subtract the discrepancy from the date milliseconds

    return (millis_since_epoch - date_millis_since_epoch); // finally, find the difference
}

Obviously the above is a very verbose implementation, but it communicates the essence of what I'm trying to do.

So, is there a simpler way to do what I described? And if not, any comments on adding this sort of functionality? I'd be happy to initiate a pull request.

dillongrove commented 11 years ago

Hello?

jacaetevha commented 11 years ago

Added comment to issue #10.

mits3 commented 10 years ago

@dillongrove This solved my issue..Great Work.

dillongrove commented 10 years ago

Glad to hear @mits3 . If I may ask, what is your use case for needing this functionality? Is it similar to mine?