Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

javascript and timezone ?

Status
Not open for further replies.

matt1970

Programmer
Oct 21, 2006
3
US
Hi,

I'd like to use javascript to obtain a user's timezone setting on their local machine. here is the code that I currently have:

var mydate = new Date();
mydate.getTimezoneOffset()/60;

this code returns a number with a decimal value. It's sort of what I want, but not examply.

One timezone might be known as "+5:45" ...how would I get javascript to show the user's timezone as "+5:45" and NOT as "5.75" ? Basically, I want the timezone to show the number to the right of the decimal as the minutes (and not as a hundredths value)

Thank you in advance for any tips. Source code is greatly appreciated.
 
>One timezone might be known as "+5:45" ...
Where, for instance?
 
In the Microsoft Windows OS, the time zone setting for the computer clock offers many options, including:


(GMT -05:00) Eastern Time (US & Canada)
(GMT +05:45) Kathmandu
(GMT +03:30) Tehran

I'd like to use javascript to obtain the values:
-05:00
+05:45
+03:30

unforutnately, the code I'm currently using doesn't provide the format that I'm looking for. For the code mydate.getTimezoneOffset()/60; ...instead of showing +03:30, it shows something like 3.5

Any suggestions on how to get the format that I want?

Thanks!
 
Okay, I see. Try this.
[tt]
var mydate = new Date();
var x=mydate.getTimezoneOffset();
var hh=Math.floor(x/60);
var mm=x-hh*60;
mm=(mm.toString().length==1)?"0"+mm:mm;
alert(hh+":"+mm);
[/tt]
 
Correction

As the bias can go both way, I should take it into consideration. Here is the correction.
[tt]
var mydate = new Date();
var x=mydate.getTimezoneOffset();
var y=Math.abs(x);
var sgn=(y==x)?"+":"-";
var hh=Math.floor(y/60);
var mm=y-hh*60;
mm=(mm.toString().length==1)?"0"+mm:mm;
var sbias=sgn+hh+":"+mm;
alert(sbias);
[/tt]
 
Upon re-read the format requested, have to add a line to auguement hh, like what done for mm.
[tt]
var mydate = new Date();
var x=mydate.getTimezoneOffset();
var y=Math.abs(x);
var sgn=(y==x)?"+":"-";
var hh=Math.floor(y/60);
var mm=y-hh*60;
[blue]hh=(hh.toString().length==1)?"0"+hh:hh;[/blue]
mm=(mm.toString().length==1)?"0"+mm:mm;
var sbias=sgn+hh+":"+mm;
alert(sbias);
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top