html - World Clock in Javascript not getting the hours properly -


i have made javascript world time clock basic. made work there little problem different cities hours not updating want. example sydney time difference uk 11 hours added 11 + hours. added condition format time 12 hours format sydneyhours > 12 sydneyhours -= 12;

but still showing 11 + whatever number of hours in localtime e.g(16:46:49);

plus suggestions improvements appreciated.

many thanks

function timenow(){  	var currenttime = new date();  	var hours = currenttime.gethours();  	var minutes = currenttime.getminutes();  	var seconds = currenttime.getseconds();  	var meridiem = "am";  	var isbhours = 5 + hours;  	var romehours = 1 + hours;  	var sydneyhours = 11 + hours;    	  	if(seconds < 10){  		seconds = "0" + seconds;  	}    	if(minutes < 10){  		minutes = "0" + minutes;  	}    	if(hours < 10){  		hours = "0" + hours;  	}    	if(sydneyhours < 10){  		sydneyhours = "0" + sydneyhours;  	}    	if(hours > 12){  		hours = hours - 12;  		meridiem = "pm";  	}    	if(isbhours > 12){  		isbhours = isbhours - 12;  		meridiem = "pm";  	}    	if(romehours > 12){  		romehours = romehours - 12;  		meridiem = "pm";  	}    	if(sydneyhours > 12){  		sydneyhours = sydneyhours - 12;  		meridiem = "pm";  	}  	          	var localclock = document.getelementbyid('localclock');    	localclock.innertext = hours + ":" + minutes + ":" + seconds + " " + meridiem;  	localclock.innertext += "\n" + "local time";    	var isbclock = document.getelementbyid('isbclock');  	isbclock.innertext = isbhours + ":" + minutes + ":" + seconds + " " + meridiem;  	isbclock.innertext += "\n" + "islamabad time";    	var sydneyclock = document.getelementbyid('sydneyclock');  	sydneyclock.innertext = sydneyhours + ":" + minutes + ":" + seconds + " " + meridiem;  	sydneyclock.innertext += "\n" + "sydney time";        	var romeclock = document.getelementbyid('romeclock');  	romeclock.innertext = romehours + ":" + minutes + ":" + seconds + " " + meridiem;  	romeclock.innertext += "\n" + "rome time";  }    setinterval(timenow, 1000);
#localclock {  	background: #87d4ed;  }  #isbclock {  	background: #eddb11;  }    #sydneyclock {  	background: #f27d66;  }    #romeclock {  	background: #f09e2b;  }    #worldclock {  	width: 800px;  	margin: 50px auto;    }  #localclock, #isbclock, #sydneyclock, #romeclock {  	font-size: 40px;  	font-family: arial, monospace;      position: relative;      top: 0; left: 0; right: 0; bottom: 0;      color: #fff;      padding-top: 70px;      text-align: center;      width: 400px;      height: 200px;      float: left;  }
<div id="worldclock">    	<div id="localclock"></div>  	<div id="isbclock"></div>  	<div id="sydneyclock"></div>  	<div id="romeclock"></div>  	   </div>

your initial problem, related sydney time because not accounting fact hours variable going on 24.

when local time in pm, add 11 hours time difference , subtract 12 try , convert 12 hour clock.

here's whats happening

16 //local hours + 11 = 27 // time zone adjustment -12 = 15 // attempt convert 12 hour clock 

the easiest way compensate through use of modulus operator.

var sydneyhours = (11 + hours) % 24; 

this open new problem sydney display 03:43:23 pm when because using singular meridiem variable timezones , if 1 of them pm set pm. situation learn concept of objects. can create new object each city , assign them own hour , meridiem property values , read them separately afterwards.


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -