5/16/2023 0 Comments Utc time calculator![]() ![]() Return ts.Seconds = 1 ? "one second ago" : ts.Seconds + " seconds ago" Var ts = new TimeSpan( - yourDate.Ticks) ĭouble delta = Math.Abs(ts.TotalSeconds) Jeff, your code is nice but could be clearer with constants (as suggested in Code Complete). Var time_string_utc_epoch = Date.parse(some_server_variable) No need to guess the user's country or even adjust your timestamp, so long as you trust the user's local browser/computer time zone.Īgain, the short version: var time_string_utc = some_server_variable // UTC time from server Which, if you are in my timezone using the UTC timestamp I started off with: īut you can use various javascript methods to format the time into something much more pleasant looking. As a simple experiment, you can use: document.write(time_utc) Now that you have the object, you can output it to the user as you see fit. The last line line uses setTime method, which sets a Date object from an epoch timestamp. The third variable creates the unset Date object. The second variable uses the Date.parse() method to convert the string to an epoch timestamp. The first variable assumes you have passed the timestamp string to that variable from the server. A quick explanation of what happens above: ![]() Var time_string_utc_epoch = Date.parse(time_string_utc) Īt this point, you have a javascript date object set to your UTC timestamp. So the javascript would be: var time_string_utc = some_server_variable // timestamp from server You should remember that if a user is from Tokyo and is viewing your website in Spain, they will see the timestamp in Tokyo time unless they've adjusted their computer's clock. The javascript can then parse that timestamp and it will output the date and time adjusted to the browser's time zone (which is determined by the computer hosting the browser). So, first you get the server to output the above to your web page's javascript. ![]() So in my case, since I'm at -0500 hours from GMT, the same time above would be: Such that where I am, the time would be: T05:52:33īut when the time is not local, but UTC, you add a Z to the end to denote this. The basic syntax of RFC 3339 is: YYYY-MM-DDTHH:MM:SS If you (and your website) are comfortable with javascript, there is a very easy way to accomplish this.įirst, on the server side, you would have the UTC date/time formatted in RFC 3339 format (the standard for internet time used by, among other protocols, icalendar). ![]()
0 Comments
Leave a Reply. |