Convert getTimezoneOffset(); into Standard GMT Statement

[FONT=“Georgia”]Hi.

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);

[FONT=“Georgia”]Any ideas ?

[/FONT]

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)

Correct. You know you’re stuff.

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); // 

[FONT=“Georgia”]I don’t understand the part about multiplying by -1 … wouldn’t that give you a negative number regardless ?

Therefore if someone on Australia ran this wouldn’t they get a negative GMT too (whereas they should be positive) ?[/FONT]

[edit]Ore wait no.

I figured it out.

Thanks, man. :tup:[/edit]

[FONT=“Georgia”]Another question.

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).

[/FONT]

Just manipulate it as a string. Nobody is going to be at more than GMT +/-12, so:

var hour = 9;
hour = String(hour);
if (hour.length === 1) hour = '0' + hour;
hour += '00';

[FONT=“Georgia”]Sort of cheating but hell it works !

Thanks a lot, Raf’.

:tup:

[/FONT]

Had same issue just found out the solution, thanks to you!

What how to take care of DST for example CST now days is -5.00 but after Day light saving its -6.00.

Is there any code.

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.