You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
functiondistance(date){varUTC_date_string=newDate().toUTCString();// get current UTC Time in string formvarmillis_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. */vardate_millis=date.getTime();// convert passed in date to milliseconds (still wrong timezone)varminutes_off=date.getTimezoneOffset();// get the number of minutes this timezone differs from UTC timevarmillis_off=minutes_off*60*1000;// convert from minutes to millisecondsvardate_millis_since_epoch=date_millis-millis_off;// subtract the discrepancy from the date millisecondsreturn(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.
The text was updated successfully, but these errors were encountered:
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 thedistance
function in the plugin as follows: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.
The text was updated successfully, but these errors were encountered: