tag:blogger.com,1999:blog-15626356.post113671618563536323..comments2012-10-17T10:09:12.903-07:00Comments on ecmanaut: AJAX × Date × Time × Time zones - best practicesJohan Sundströmhttp://www.blogger.com/profile/04076097346172610543noreply@blogger.comBlogger25125tag:blogger.com,1999:blog-15626356.post-58935295414819434032007-08-16T18:07:00.000-07:002007-08-16T18:07:00.000-07:00For WordPress : http://trevorcreech.com/geekery/wo...For WordPress : http://trevorcreech.com/geekery/wordpress/user-relevant-timezonesSingpolymahttps://www.blogger.com/profile/14267910391550235126noreply@blogger.comtag:blogger.com,1999:blog-15626356.post-28294952475530024582007-06-11T18:54:00.000-07:002007-06-11T18:54:00.000-07:00getFullYears should read getFullYear.getFullYears should read getFullYear.Johan Sundströmhttps://www.blogger.com/profile/04076097346172610543noreply@blogger.comtag:blogger.com,1999:blog-15626356.post-45528748016715831462007-06-11T17:31:00.000-07:002007-06-11T17:31:00.000-07:00Here's a test page that shows the script not worki...Here's a test page that shows the script not working...<BR/><BR/>http://www.spiffyd.com/index2.php<BR/><BR/>Typo?Denis Lamhttps://www.blogger.com/profile/12409800788613392215noreply@blogger.comtag:blogger.com,1999:blog-15626356.post-24780268222651726422007-06-11T03:09:00.000-07:002007-06-11T03:09:00.000-07:00The likeliest issue is the typo that used to be in...The likeliest issue is the typo that used to be in the definition above(!) -- if it doesn't work now, a URL to your test page would be more helpful.<BR/><BR/>(And no, there is no need for any onload callbacks.)Johan Sundströmhttps://www.blogger.com/profile/04076097346172610543noreply@blogger.comtag:blogger.com,1999:blog-15626356.post-20735475267860107512007-06-10T21:57:00.000-07:002007-06-10T21:57:00.000-07:00I also forgot to ask, is there a need for any onlo...I also forgot to ask, is there a need for any onload functions in the BODY tag?Denis Lamhttps://www.blogger.com/profile/12409800788613392215noreply@blogger.comtag:blogger.com,1999:blog-15626356.post-52958822396857191802007-06-10T21:53:00.000-07:002007-06-10T21:53:00.000-07:00Forget about my reference to creating a var timeIn...Forget about my reference to creating a var timeIngeter. So what I did was placed formatDebug and formatTime functions in the HEAD.<BR/><BR/>And in the BODY, I have a script with the following document.write script:<BR/><BR/>document.write(formatTime(parseInt(((new Date).getTime()))));<BR/><BR/>But I do not get any output. However, formatDebut(...) works. Is there something wrong with the document.write? Thank you for your response. I really appreciate it!Denis Lamhttps://www.blogger.com/profile/12409800788613392215noreply@blogger.comtag:blogger.com,1999:blog-15626356.post-78461105001886288842007-06-08T13:52:00.000-07:002007-06-08T13:52:00.000-07:00Put the document.write() where you want the output...Put the document.write() where you want the output (i e not in the head tag), because that is where it will show up. Also, you can't run document.write after the page is fully loaded, because then it will replace the document.Johan Sundströmhttps://www.blogger.com/profile/04076097346172610543noreply@blogger.comtag:blogger.com,1999:blog-15626356.post-10937537285925807012007-06-08T08:27:00.000-07:002007-06-08T08:27:00.000-07:00Hello ecmacnaut, I am a novice with js and I'm hav...Hello ecmacnaut, I am a novice with js and I'm having some trouble with the script to get the date/time displayed. <BR/><BR/>I have your script functions inserted in the HEAD section and I added...<BR/><BR/>var timeInteger = parseInt(((new Date).getTime()/1e3));<BR/><BR/>I just can't get document.write to work. Am I missing something? Thank you in advance.Denis Lamhttps://www.blogger.com/profile/12409800788613392215noreply@blogger.comtag:blogger.com,1999:blog-15626356.post-1170408257815535892007-02-02T01:24:00.000-08:002007-02-02T01:24:00.000-08:00That should be the beauty of this solution. All US...That should be the beauty of this solution. All US residents would get times reported according to their local DST rules, before and after the switch, and you won't have to update a single line of code anywhere either.Johan Sundströmhttps://www.blogger.com/profile/04076097346172610543noreply@blogger.comtag:blogger.com,1999:blog-15626356.post-1170397232001752732007-02-01T22:20:00.000-08:002007-02-01T22:20:00.000-08:00How daylight saving time change in us will effect ...<B>How daylight saving time change in us will effect javascript Date behaviour.</B>Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-15626356.post-1166735306281966732006-12-21T13:08:00.000-08:002006-12-21T13:08:00.000-08:00We ran into this exact issue for our home-grown gr...We ran into this exact issue for our home-grown group-ware. The programming lead wrote a very elegant ECMA-script solution to calculate time-zone differences and try to round them into a reasonable GMT offset for the purpose of storing events, tasks, notes, etc. (which are all stored in GMT).<BR/><BR/>Another programmer on the project didn't understand the code and proceeded to delete it and add in a time-zone option to the user settings; yuck.<BR/><BR/>Of course, while researching this, we came across Israel's DST rules - they are decided each year so as to prevent the switch from interfering with Jewish holidays (which last from sundown to sundown and the DST switch takes place at 2:00 AM).Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-15626356.post-1163439205561908552006-11-13T09:33:00.000-08:002006-11-13T09:33:00.000-08:00A project I regularly work on has international vi...A project I regularly work on has international viewers to overcome the timezone issue we took a slightly different approach.<BR/><BR/>We wanted to display all times local to the user as this is in our opinion the easiest way for them to interpret them.<BR/><BR/>The solution we came up with is a simple javascript which is called on the first visit to the site using asynchrous calls the script posts the javascript time in seconds to the server, the server does the math to find the offset between server timestamp and client timestamp, a simple php function adds/subtracts from all timestamps around the site as required.<BR/><BR/>We also allow registered users to overide by setting their timezone in their profiles.<BR/><BR/>I am also a fan of the timezone irrelvant approach many sites are now taking of displaying the offset time i.e. (25 Minutes Ago, 5 days Ago) although this can only be used where accuracy is not important and it is something we do alongside displaying the actual date of approach.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-15626356.post-1158152006280412952006-09-13T05:53:00.000-07:002006-09-13T05:53:00.000-07:00"Wed Sep 13 2006 14:42:34 GMT+0200" is what the Wi..."Wed Sep 13 2006 14:42:34 GMT+0200" is what the Win32 Firefox I write this with reports for Date.toString(), so it is apparently not pan-Gecko either.Johan Sundströmhttps://www.blogger.com/profile/04076097346172610543noreply@blogger.comtag:blogger.com,1999:blog-15626356.post-1158149309771315482006-09-13T05:08:00.000-07:002006-09-13T05:08:00.000-07:00I (sort of) solved it for Mozilla/Firefox with thi...I (sort of) solved it for Mozilla/Firefox with this code (milli contains the timestamp)<BR/>var TZ= milli.toString().replace(/^.*\((.*?)\).*$/, '$1');<BR/>if ( TZ.length > 5 ) {<BR/> TZ= milli.getTimeZoneOffset()/60;<BR/> TZ= 'UTC' + ( TZ < 0 ? '+' : '-' ) + zeropad2(Math.abs(TZ));<BR/>}<BR/><BR/>(zeropad2 is just a standard function adding a leading zero for 0-9)<BR/><BR/>With this code I get the "mediocre" names that are available through Date.toString() in Mozilla/FireBird. In Safari they are not available and so the if-part to handle that situation.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-15626356.post-1158131458190478992006-09-13T00:10:00.000-07:002006-09-13T00:10:00.000-07:00Unfortunately, javascript doesn't offer any expose...Unfortunately, javascript doesn't offer any exposed programming interfaces to time zones; not even the +/-HHMM ones, less still the mediocre PST/CEST/CET aliases (who occasionally are ambiguous, and in either case are not generally understandable to most humans) and the good America/Chicago, Europe/Vienna or Asia/Novosibirsk zone names.<BR/><BR/>The downside is that you need more information than you have to be able to pick the correct zone name/alias, so you need to take the long road via user interaction to get one. :-(Johan Sundströmhttps://www.blogger.com/profile/04076097346172610543noreply@blogger.comtag:blogger.com,1999:blog-15626356.post-1158106625513044062006-09-12T17:17:00.000-07:002006-09-12T17:17:00.000-07:00I think the article is okay. But there are still t...I think the article is okay. But there are still times where I think it would be good to display the local user's timezone name (like PST, CEST, CET...). <BR/><BR/>Imagine seeing these timestamps:<BR/>2006-02-08 21:00:03 UTC+01<BR/>2006-08-12 22:55:01 UTC+02<BR/>Both calculated at the same time on my local machine with JavaScript... Why is ti that I have +01 and +02? Simply because of Daylight Saving Time and I didn't notice. I thought of a programming issue.<BR/><BR/>Would it be<BR/>2006-02-08 21:00:03 CET<BR/>2006-08-12 22:55:01 CEST<BR/>I wouldn't have any problem<BR/><BR/>But as of now I haven't found any way to get my local timezone's name :-(Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-15626356.post-1141391471753071802006-03-03T05:11:00.000-08:002006-03-03T05:11:00.000-08:00All good ideas have limited applicability; 1) and ...All good ideas have limited applicability; 1) and 3) for instance, are clearly out of scope for this method, and clearly it's not the be all end all solution to time zone problems; it's a method to convert server time to client readable time.<BR/><BR/>Does anyone claim it's more than that? If so, I would suggest enlightening them there rather than here, as people generally tend not to read lengthy comments.<BR/><BR/>Regarding 2), I see nothing stopping a good time input widget from using the same method as here to feed a timestamp integer upstream, after having done some browser parsing of date and time in client side javascript. Again, only applicable to javascript enabled browsers and all that, but you get the idea.Johan Sundströmhttps://www.blogger.com/profile/04076097346172610543noreply@blogger.comtag:blogger.com,1999:blog-15626356.post-1139914044068308322006-02-14T02:47:00.000-08:002006-02-14T02:47:00.000-08:00First off: I would have been glad to post this com...First off: I would have been glad to post this comment to your blog post, had it been possible (feel free to add it yourself when you have found and fixed the problem).<BR/><BR/>While I'm no fierce advocate against extending HTML (and its derivates) with useful semantically named attributes, or even tags, my experience with using such derived formats in practice with browsers has been that it has not been possible to get them to render your documents in standards compliance mode, which is what you want to do to maintain sanity while styling your web page.<BR/><BR/>Last time I researched that (a few years ago, admittedly) it was not possible, even if you properly defined and linked to a DTD of your own describing the tags and attributes, inheriting the rest from i e XHTML Strict, not to end up in quirks mode.Johan Sundströmhttps://www.blogger.com/profile/04076097346172610543noreply@blogger.comtag:blogger.com,1999:blog-15626356.post-1139911499503297622006-02-14T02:04:00.000-08:002006-02-14T02:04:00.000-08:00Johan, Daniel,You're both right, of course. I don...Johan, Daniel,<BR/><BR/>You're both right, of course. I don't particularly have a problem with non-standard attributed when they're useful, but hCalendar proves that I should have thought a bit harder before deciding on a format.<BR/><BR/>I've <A HREF="http://mikewest.org/blog/id/21" REL="nofollow">updated the class to support the hCalendar format</A>, and I'm interested in your feedback regarding my method.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-15626356.post-1139781337228120932006-02-12T13:55:00.000-08:002006-02-12T13:55:00.000-08:00You're welcome. Your pick of solution is another t...You're welcome. Your pick of solution is another tradeoff, dropping an ugly noscript for getting a document which instead won't validate (the custom argument name). If I were to go down that route, I would have picked the class name to carry all the data, i e class="PerfectTime 1111396060" or even class="PerfectTime:1111396060". (That would actually even be easier for you to scan for and parse, relieving you of some code baggage.)Johan Sundströmhttps://www.blogger.com/profile/04076097346172610543noreply@blogger.comtag:blogger.com,1999:blog-15626356.post-1139774880283117332006-02-12T12:08:00.000-08:002006-02-12T12:08:00.000-08:00Johan,I love this concept.I took some of the ideas...Johan,<BR/><BR/>I love this concept.<BR/><BR/>I took some of the ideas in this post, as well as RedHanded's code, and put together an unobtrusive and light JavaScript class that makes this entire process fairly transparent, and maintains the semantic quality of the HTML code (I'm simply not a fan of NOSCRIPT tags, nor of inline document.write).<BR/><BR/>The class is avaliable here: <A HREF="http://mikewest.org/blog/id/20" REL="nofollow">http://mikewest.org/blog/id/20</A>.<BR/><BR/>Thanks for the inspiration!Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-15626356.post-1139507891188394952006-02-09T09:58:00.000-08:002006-02-09T09:58:00.000-08:00While I admit this has very much wider applicabili...While I admit this has very much wider applicability than XMLHttpRequest, it is nevertheless an issue that typically shows up with AJAX applications, and one addressable by this solution in that context, as they still rely on Javascript availability.<BR/><BR/>So it's more of a heads-up for the typical target audience that needs to learn this technique, than a marker for limiting the article scope.Johan Sundströmhttps://www.blogger.com/profile/04076097346172610543noreply@blogger.comtag:blogger.com,1999:blog-15626356.post-1139495825134842882006-02-09T06:37:00.000-08:002006-02-09T06:37:00.000-08:00I don't get it... why did you put "Ajax" in the ti...I don't get it... why did you put "Ajax" in the title of your post, if it's got nothing to do with XMLHTTPRequest? Hmmm... anyways, regarding timezone offsets, if your website is hosted on a Unix server then you can use the built-in TZ timezone and daylight saving time data. That's what I did to make this <A HREF="http://date-heure.com/en/" REL="nofollow">world dates and times</A> page in PHP.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-15626356.post-1137367448791367422006-01-15T15:24:00.000-08:002006-01-15T15:24:00.000-08:00Yet another javascript library? It's one method, t...Yet another javascript library? It's one method, that formats a date. (<a href="http://redhanded.hobix.com/inspect/showingPerfectTime.html">RedHanded</a>'s take on it is even nicer, if larger.) And if your client side code already uses some library (Mochikit, Dojo, Prototype), it probably already has the method built in, too.<BR/><BR/>The point above is not in placing blame, but to always get this to work right, and without burdening the visitor. In the (hopefully or probably rather rarely encountered in practice) case you picked, when a visitor has configured a time zone that your framework can't handle properly, nobody will think twice about <I>why</I> your site doesn't work; that is not their problem to ponder. Either it works, or it does not, and if it does not, the flaw is reflected back on you, not on libc, or ASP.net, or whatever your staff of programmers would nail it down to if they were alerted of the issue.<BR/><BR/>Your more fundamental question is right on the mark -- there are lots of times when you should <I>not</I> format times in the visitor's frame of reference. It largely depends on what the time reading is used for, and how the information is most likely to be used by the visitor, as you conclude.<BR/><BR/>There are three time zones at play in that example, though; mine, yours and the web server (in the odd case even a fourth database server) time zone. The naïve implementation will show you server time, which is never ever interesting. In actual cases, that is often the same time zone as that of the narrator, in which cases the flaw is less severe, as that time often has some relevance.<BR/><BR/>Anyway, to summarize: listing time will always need reflection about what the time reading is used for, who will read it, and what it will convey to her. In many cases, more than one way of stating time can be useful; "it's midnight now where I am" and "this was posted six hours ago", can both be useful bits of information to a site visitor. "You will most likely find me online for live conversation between 10 and 13, your time" could be another, in some more forum-like setting.<BR/><BR/>My article doesn't address when what choice (or choices) is the right one to make; that is all up to you. Many times it will make perfect sense to list multiple times, clearly marking each one with its frame of reference. But I do want to raise the issue of the options at hand, and push for using the technical means on offer for showing visitor time, when that is what we set out to do.<BR/><BR/>Because today, many go to great lengths to get it just almost right, and typically at the cost of visitor convenience. The solution isn't very tricky, as I believe I showed above, once you have been made aware of it.Johan Sundströmhttps://www.blogger.com/profile/04076097346172610543noreply@blogger.comtag:blogger.com,1999:blog-15626356.post-1137360537027981252006-01-15T13:28:00.000-08:002006-01-15T13:28:00.000-08:00Interesting thought... but why would you want to p...Interesting thought... but why would you want to plug yet another Javascript library into every page you're sending, instead of having your programming language/framework sort out the fine details (and blame them if your Tadjmbekistan user keeps getting the wrong time)?<BR/>And, more fundamentally: does your reader really want to see the timestamps converted to his own timezone? I for one don't. It means nothing to me that you've posted something at 02:21 local time (local for me). It means I was sleeping at the time you wrote this. On the other hand, if I see you were posting at 02:21 *your* local time, I could deduce that you were fighting against sleep, sitting in the dark of night, when you were posting. If you made a typo, I'm more likely to forgive you -- it was late at your end.Anonymousnoreply@blogger.com