I’m using Javascript’s getTimezoneOffset() to get the client’s current Timezone but of course this only returns the Timezone in minutes and doesn’t tell you if it’s more or less than Greenwich Meridian Time.
Converting the minutes to hours is easy enough… just divide by 60… but how do I figure out if there should be a plus or a minus sign (more or less than GMT) before the output ?[/FONT]
var currentTime = new Date();
var currentTimezone = currentTime.getTimezoneOffset();
currentTimezone = (currentTimezone/60);
It should return a positive number if you’re behind GMT and a negative one if you’re after it (which seems odd, surely it should be the other way around). So, you would just do this:
var currentTime = new Date();
var currentTimezone = currentTime.getTimezoneOffset();
currentTimezone = (currentTimezone/60) * -1;
var gmt = 'GMT';
if (currentTimezone !== 0) {
gmt += currentTimezone > 0 ? ' +' : ' ';
gmt += currentTimezone;
}
alert(gmt); // For Trinidad this would be something like 'GMT -4' (I'm guessing there)
This is something I thought would have been obvious but it wasn’t…
How do I get a figure to a decided number of places before the decimal point ?
Example (in this case)… how can I convert “4” to “0400”[/FONT]
Edit:
Which is actually four hundred but it seems to be the standard way of displaying Timezones for whatever reason.
[FONT=“Georgia”]I played with decimals but that only gives me places after the decimal point.
Therefore I was able to get… “4.0000” which isn’t very helpful (but funny when I realised how silly it was to think it’d work. Goes to show how it’s been since I’ve been in a math class).
The timezone offsets in JavaScript are in munites west of GMT so that timezones east of GMT have a negative sign on the front and you need to divide by 60 to get hours. Timezones actually run from 12 hours west to 14 hours east of GMT (when you take daylight saving into account).
JavaScript doesn’t provide a daylight saving flag but you can calculate one provided that the location isn’t on daylight saving for the whole year.
The one thing about timezones and daylight saving that JavaScript cannot access or calculate it the timezone name.
Take a look at http://javascript.about.com/library/bldateformat.htm for a date method that I wrote for JavaScript that can easily produce date and time information formatted however you want it formatted with a single call.