Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 05-11-2012, 04:13 AM   PM User | #16
daleala
New Coder

 
Join Date: May 2012
Location: Australia
Posts: 10
Thanks: 5
Thanked 0 Times in 0 Posts
daleala is an unknown quantity at this point
Next batch of code.

If I have done it correctly, it should show the current time in Victoria, while taking daylight savings into consideration.

Have I done the nested if conditions correctly?

Code:
if
    {
        if
            {
                blah1
            }
        else
           {
               blah2
           }
    }
And is that a good way of doing nested if's? (Or even an acceptable way?

Code below in its entirety.

Code:
<html>
<head>
<script type="text/javascript">
function startVic() {



var nowvica = new Date();  
var yrvica = nowvica.getYear();
var DSTusedvica = false;

var janvica = new Date(yrvica,0,1);  // 1st January in current year
var janoffvica = janvica.getTimezoneOffset();
var julvica = new Date(yrvica,6,1);  // 1st July in current year
var juloffvica = julvica.getTimezoneOffset();
// alert ("January " + janoff +  "          July " + juloff); // for testing
if (janoffvica != juloffvica) {
DSTusedvica = true;
}

if (DSTusedvica) {
var ntzovica = nowvica.getTimezoneOffset();
if (ntzovica == juloffvica) {
 // DST is in operation
}
else {
 // DST is not in operation
}
}






var nowvic = new Date();
var hvic=nowvic.getUTCHours();
var minvic=nowvic.getUTCMinutes();
var svic=nowvic.getUTCSeconds();
if (hvic>12) {hvic-=12}
if (hvic==0) {hvic=12};

if (ntzovica == juloffvica)
	{
		if (hvic<=2)
			{
					hvic=hvic+10
			}
		else
			{
					hvic=hvic-2
			}
	}
else
	{
		if (hvic<=1)
			{
				hvic=hvic+11
			}
		else
			{
				hvic=hvic-1
			}
	}



if (minvic<10) {minvic="0"+minvic}
if (svic<10) {svic="0"+svic}
document.getElementById("timevic").innerHTML=hvic+":"+minvic+":"+svic;
var timvic = setTimeout("startVic()",1000);
}
</script>
</head>
<body onload="startVic()">
<span id="timevic"></span>
</body>
</html>

Last edited by daleala; 05-11-2012 at 04:15 AM..
daleala is offline   Reply With Quote
Old 05-13-2012, 11:26 PM   PM User | #17
daleala
New Coder

 
Join Date: May 2012
Location: Australia
Posts: 10
Thanks: 5
Thanked 0 Times in 0 Posts
daleala is an unknown quantity at this point
Is there any way I can add "setTimezone("AEST");" or setTimezone("Australia/Melbourne");" into the coding somewhere or something similar to the DST check (see above) so it checks if it is daylight savings or not in Victoria, regardless of the timezone it is running from?
daleala is offline   Reply With Quote
Old 05-14-2012, 07:39 AM   PM User | #18
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,036
Thanks: 197
Thanked 2,411 Times in 2,389 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by daleala View Post
Is there any way I can add "setTimezone("AEST");" or setTimezone("Australia/Melbourne");" into the coding somewhere or something similar to the DST check (see above) so it checks if it is daylight savings or not in Victoria, regardless of the timezone it is running from?
No. As I said before, it is possible to detect whether DST is used and is currently in operation in the user's own country, but AFAIK it is not possible for a user in one country to detect whether DST is currently in operation in some other country, e.g. Japan or Argentina.

In any case, why does it matter to a user in (say) New Zealand whether or not DST is currently in operation in Victoria Australia?
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
Philip M is offline   Reply With Quote
Old 05-14-2012, 08:08 AM   PM User | #19
daleala
New Coder

 
Join Date: May 2012
Location: Australia
Posts: 10
Thanks: 5
Thanked 0 Times in 0 Posts
daleala is an unknown quantity at this point
Unless I am mistaken, because we are telling the clock to go to UTC time then add 10 hours, during daylight savings time (+11 hours instead of +10) the clock would be 1 hour off. It's not hugely important for this implementation as it may only get used in Victoria, but something I'll try to look into as the next upgrade so it can actually get used cross-state (and then post the code here for an open/closed taking DST into consideration script)

I'll post the entire working code in the next day or so - as best as I can tell it is all working, I just want to bug hunt and double-check myself.

Cheers.
daleala is offline   Reply With Quote
Old 05-14-2012, 08:12 AM   PM User | #20
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,036
Thanks: 197
Thanked 2,411 Times in 2,389 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Do the states of Australia have different dates of implementation of DST?

I now see that those states that do use DST use the same dates, but not all Australian states use DST.
And there are three different time zones in Australia.
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.

Last edited by Philip M; 05-14-2012 at 08:17 AM..
Philip M is offline   Reply With Quote
Old 05-14-2012, 11:08 PM   PM User | #21
daleala
New Coder

 
Join Date: May 2012
Location: Australia
Posts: 10
Thanks: 5
Thanked 0 Times in 0 Posts
daleala is an unknown quantity at this point
Quote:
Originally Posted by Philip M View Post
Do the states of Australia have different dates of implementation of DST?

I now see that those states that do use DST use the same dates, but not all Australian states use DST.
And there are three different time zones in Australia.
Yeah, if they all used DST at the same time it would not be an issue at all!
There's been a few times where the use of DST has been questioned by us Aussies, but nothing has happened of it. Go figure!

I was busy looking for a way of working out how to find out the DST in a specific timezone clientside - I found a few ways server-side using Ruby on Rails or PHP but nothing in Javascript.
I'll keep looking and see what I can find, then update this thread.
daleala is offline   Reply With Quote
Old 05-15-2012, 08:06 AM   PM User | #22
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,036
Thanks: 197
Thanked 2,411 Times in 2,389 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by daleala View Post
I was busy looking for a way of working out how to find out the DST in a specific timezone clientside - I found a few ways server-side using Ruby on Rails or PHP but nothing in Javascript.
As I have already tried to explain, it is not possible using client-side code.
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
Philip M is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:42 PM.


Advertisement
Log in to turn off these ads.