0 Replies - 1293 Views - Last Post: 12 June 2011 - 05:21 PM

#1 maniacalsounds  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 118
  • View blog
  • Posts: 474
  • Joined: 27-June 10

Simple Clock

Posted 12 June 2011 - 05:21 PM

Description: Simply add this script into your code, and you can call the clock() function. It takes one parameter: where. Simply pass the id of the place where you want the clock to be put, it will edit the HTML, and do it for you.This is a simple clock function.
<script type="text/javascript">
	function clock(where) {
		var now = new Date();
		var hour = now.getHours().toString();
		
		switch(hour) {
			case "0":
				hour = "12";
				break;
			case "13":
				hour = "1";
				break;
			case "14":
				hour = "2";
				break;
			case "15":
				hour = "3";
				break;
			case "16":
				hour = "4";
				break;
			case "17":
				hour = "5";
				break;
			case "18":
				hour = "6";
				break;
			case "19":
				hour = "7";
				break;
			case "20":
				hour = "8";
				break;
			case "21":
				hour = "9";
				break;
			case "22":
				hour = "10";
				break;
			case "23":
				hour = "11";
				break;
		}
		
		var min = now.getMinutes().toString();
		
		switch (min) {
			case "0":
				min = "00";
				break;
			case "1":
				min = "01";
				break;
			case "2":
				min = "02";
				break;
			case "3":
				min = "03";
				break;
			case "4":
				min = "04";
				break;
			case "5":
				min = "05";
				break;
			case "6":
				min = "06";
				break;
			case "7":
				min = "07";
				break;
			case "8":
				min = "08";
				break;
			case "9":
				min = "09";
				break;
		}
		
		var sec = now.getSeconds().toString();
		
		switch (sec) {
			case "0":
				sec = "00";
				break;
			case "1":
				sec = "01";
				break;
			case "2":
				sec = "02";
				break;
			case "3":
				sec = "03";
				break;
			case "4":
				sec = "04";
				break;
			case "5":
				sec = "05";
				break;
			case "6":
				sec = "06";
				break;
			case "7":
				sec = "07";
				break;
			case "8":
				sec = "08";
				break;
			case "9":
				sec = "09";
				break;
		}
		
		var time = hour + ":" + min + ":" + sec;
		
		document.getElementById(where).innerHTML = time;
		
		setTimeout("clock('" + where + "')", 1000);
	}
</script>


Is This A Good Question/Topic? 0
  • +

Page 1 of 1