How to Implement Dynamic Timestamp in Web Page?

Posted by Morgan Cheng on Stack Overflow See other posts from Stack Overflow or by Morgan Cheng
Published on 2011-01-15T05:20:28Z Indexed on 2011/01/15 6:53 UTC
Read the original article Hit count: 213

Filed under:
|

In Facebook and twitter, we can see that there is a timestamp like "23 seconds ago" or "1 hour ago" for each event & tweet. If we leave the page for some time, the timestamp changes accordingly.

Since it is possible that user machine doesn't have same system time as server machine, how to make the dynamic timestamp accurate?

My idea is: It is always based on server time. When request is sent to server for the web page, timestamp T1 (seconds to 1970/1/1) is rendered into inline javascript variable. The displayed timestamp ("23 seconds ago") is calculated by T1 instead of local time.

I'm not sure whether this is how Facebook/Twitter do it. Is there any better idea?

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about web