The simple timezone clock function falls into incorrect condition and I have no idea why

60 Views Asked by At

I implemented a simple timezone clock and it somehow does not change time based on different timezone, after a few debugging I found out it falls to incorrect "if" condition. Here's the code:

var zone = "ho";

alert("1"+zone);
var ft;
var dt = new Date();
var def = dt.getTimezoneOffset()/60;
var gmt = (dt.getHours() + def);
var ending = ":" + IfZero(dt.getMinutes()) + ":" +  IfZero(dt.getSeconds());            

alert("2"+zone);

if(zone = "local"){
    ft = (IfZero(dt.getHours()) + ":" + IfZero(dt.getMinutes()) + ":" + IfZero(dt.getSeconds()));
    alert("firelocal");
}else if(zone = "GMT"){
    var _GMT =check24(((gmt) > 24) ? ((gmt) - 24) : (gmt));
    ft = (IfZero(_GMT) + ":" + IfZero(dt.getMinutes()) + ":" + IfZero(dt.getSeconds()));
}else if(zone = "eniw"){
    var eniw =check24(((gmt + (24-12)) > 24) ? ((gmt + (24-12)) - 24) : (gmt + (24-12)));
    ft = (IfZero(eniw) + ending);
}else if (zone = "ho"){
    var ho =check24(((gmt + 8) > 24) ? ((gmt + 8) - 24) : (gmt + 8));
    ft = (IfZero(ho) + ending);
    alert("fireho");
}else{
    ft = "undefined";
}

alert("3"+zone);

document.getElementById("worldclock").innerHTML = ft;
setTimeout("worldclock()", 5000);

And here's the output from alert:

1ho
2ho
firelocal
3local

I'm really confused right now since I'm not sure how does the function falls into if(zone = "local") condition even though I explicitly initialized zone="ho" for debugging purpose.

Can someone tell me what's wrong with my function. Sorry if the question is stupid, I just started learning javascript.

1

There are 1 best solutions below

0
On BEST ANSWER

You have:

if (zone = "local")

which makes zone take the value of "local". Change to:

if (zone === "local")