Friday, August 3, 2012

Re: Working with Dates/Timezones

Jens,

Here is the TZ script from my hobby running site. It is a tad hackish (from 4 years ago), set to update a hidden select menu in forms. I have not yet had any complaints about it's output, but I don't promise anything either. Hopefully you might find it helpful and can make a JSNI derivative if it meets your needs.


Sincerely,
Joseph
// original script by Josh Fraser (http://www.onlineaspect.com)  function calculate_time_zone() {      var rightNow = new Date();      var jan1 = new Date(rightNow.getFullYear(), 0, 1, 0, 0, 0, 0);  // jan 1st      var june1 = new Date(rightNow.getFullYear(), 6, 1, 0, 0, 0, 0); // june 1st      var temp = jan1.toGMTString();      var jan2 = new Date(temp.substring(0, temp.lastIndexOf(" ")-1));      temp = june1.toGMTString();      var june2 = new Date(temp.substring(0, temp.lastIndexOf(" ")-1));      var std_time_offset = (jan1 - jan2) / (1000 * 60 * 60);      var daylight_time_offset = (june1 - june2) / (1000 * 60 * 60);      var dst;      if (std_time_offset == daylight_time_offset) {          dst = "0"; // daylight savings time is NOT observed      } else {          // positive is southern, negative is northern hemisphere          var hemisphere = std_time_offset - daylight_time_offset;          if (hemisphere >= 0)              std_time_offset = daylight_time_offset;          dst = "1"; // daylight savings time is observed      }      var i;      // check just to avoid error messages (hidden select posted with forms)      if (document.getElementById('timezone_name')) {          for (i = 0; i < document.getElementById('timezone_name').options.length; i++) {              if (document.getElementById('timezone_name').options[i].value == convert(std_time_offset)+","+dst) {                  document.getElementById('timezone_name').selectedIndex = i;                  break;              }          }      }  }    function convert(value) {      var hours = parseInt(value);         value -= parseInt(value);      value *= 60;      var mins = parseInt(value);         value -= parseInt(value);      value *= 60;      var display_hours = hours;      // handle GMT case (00:00)      if (hours == 0) {          display_hours = "00";      } else if (hours > 0) {          // add a plus sign and perhaps an extra 0          display_hours = (hours < 10) ? "+0"+hours : "+"+hours;      } else {          // add an extra 0 if needed           display_hours = (hours > -10) ? "-0"+Math.abs(hours) : hours;      }        mins = (mins < 10) ? "0"+mins : mins;      return display_hours+":"+mins;  }

--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To post to this group, send email to google-web-toolkit@googlegroups.com.
To unsubscribe from this group, send email to google-web-toolkit+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate