Using Javascript Date Function

First posted 28/11/2011

Aku dah compile kan sebahagian daripada Date() function objects dalam satu html file ni. So boleh tengok lah apa object ni output kan bila dipanggil.

Pandai pandai lah edit guna kreativiti untuk hasilkan output yang berguna.

Download full script: http://pastebin.com/Z0c0iW9B

<!-- Codes are freely distributed 

Source:
http://www.w3schools.com/js/js_obj_date.asp
http://www.web-source.net/web_development/javascript_date.htm
http://www.quackit.com/javascript/javascript_date_and_time_functions.cfm

-->

<html>
<head><title>Some of the Javascript Date() Objects</title>

<script type="text/javascript" language="Javascript">
	function date(){
		document.write("<b>Date() returns: </b>" + "<br/>");
		document.write(Date());
		document.write("<hr>");
	}
	
	function get_date(){
		var d = new Date();
		document.write("<b>getDate() returns: </b>" + "<br/>" + d.getDate() + "<br>");
		document.write("<b>getDate()+3 returns: </b>" + "<br/>" + (d.getDate()+3) + "<hr>");
	}
	
	function get_day(){
		var d = new Date();
		document.write("<b>getDay() returns: </b>" + "<br/>" + d.getDay() + "<br>");
		
		//To display day in words
		var weekday = new Array(7);
		weekday[0]="Sunday";
		weekday[1]="Monday";
		weekday[2]="Tuesday";
		weekday[3]="Wednesday";
		weekday[4]="Thursday";
		weekday[5]="Friday";
		weekday[6]="Saturday";
		
		document.write("<b>Using array to assign days</b> <br>Today is " + weekday[d.getDay()] + "<hr>");
	}
	
	function get_fullYear(){
		var d = new Date();
		document.write("<b>getFullYear() returns: </b>" + "<br/>" + d.getFullYear() + "<hr>");
	}
	
	function get_hours(){
		var d = new Date();
		document.write("<b>getHours() returns: </b>" + "<br/>" + d.getHours() + "<hr>");
	}
	
	function get_milliseconds(){
		var d = new Date();
		document.write("<b>getMilliseconds() returns: </b>" + "<br/>" + d.getMilliseconds() + "<hr>");
	}
	
	function get_minutes(){
		var d = new Date();
		document.write("<b>getMinutes() returns: </b>" + "<br/>" + d.getMinutes() + "<hr>");
	}
	
	function get_month(){
		var d = new Date();
		document.write("<b>getMonths() returns: </b>" + "<br/>" + d.getMonth() + "<br>");
	
		var month=new Array(12);
		month[0]="January";
		month[1]="February";
		month[2]="March";
		month[3]="April";
		month[4]="May";
		month[5]="June";
		month[6]="July";
		month[7]="August";
		month[8]="September";
		month[9]="October";
		month[10]="November";
		month[11]="December";

		document.write("<b>Using array to assign months</b> <br>Current month is " + month[d.getMonth()] + "<hr>");
	}
	
	function get_seconds(){
		var d = new Date();
		document.write("<b>getSeconds() returns: </b>" + "<br/>" + d.getSeconds() + "<hr>");
	}
	
	function get_time(){
		var d = new Date();
		document.write("<b>getTime() returns: </b>" + "<br/>" + d.getTime() + " milliseconds since 1970/01/01" + "<br>");
		
		var minutes=1000*60;
		var hours=minutes*60;
		var days=hours*24;
		var years=days*365;
		
		var d=new Date();
		var t=d.getTime();
		var y=t/years;
		document.write("It's been: " + y + " years since 1970/01/01!" + "<hr>");
	}
	
	function get_timezoneOffset(){
		var d = new Date()
		var gmtHours = -d.getTimezoneOffset()/60;
		document.write("<b>getTimezoneOffset() returns: </b>" + "<br/>" + "The local time zone is: GMT " + gmtHours + "<hr>");
	}
	
	/////////////////////////DATE AND TIME////////////////////////////////////////////////////
	
	
	function getCalendarDate()
	{
	   var months = new Array(13);
	   months[0]  = "January";
	   months[1]  = "February";
	   months[2]  = "March";
	   months[3]  = "April";
	   months[4]  = "May";
	   months[5]  = "June";
	   months[6]  = "July";
	   months[7]  = "August";
	   months[8]  = "September";
	   months[9]  = "October";
	   months[10] = "November";
	   months[11] = "December";
	   var now         = new Date(); //similar as Including file in C++/Java/Python etc
	   var monthnumber = now.getMonth();
	   var monthname   = months[monthnumber];
	   var monthday    = now.getDate();
	   var year        = now.getYear();
	   if(year < 2000) { year = year + 1900; }
	   var dateString = monthname +
						' ' +
						monthday +
						', ' +
						year;
	   return dateString;
	} // function getCalendarDate()

	function getClockTime()
	{
	   var now    = new Date();
	   var hour   = now.getHours();
	   var minute = now.getMinutes();
	   var second = now.getSeconds();
	   var ap = "AM";
	   if (hour   > 11) { ap = "PM";             }
	   if (hour   > 12) { hour = hour - 12;      }
	   if (hour   == 0) { hour = 12;             }
	   if (hour   < 10) { hour   = "0" + hour;   }
	   if (minute < 10) { minute = "0" + minute; }
	   if (second < 10) { second = "0" + second; }
	   var timeString = hour +
						':' +
						minute +
						':' +
						second +
						" " +
						ap;
	   return timeString;
	} // function getClockTime()
</script>

</head>

<body>
	<center><h1>JAVASCRIPT DATE() FUNCTION</h1></center>
	<hr>
	<script type="text/javascript">
		date();
		get_date();
		get_day();
		get_fullYear();
		get_hours();
		get_milliseconds();
		get_minutes();
		get_month();
		get_seconds();
		get_time();
		get_timezoneOffset();
		
		document.write("<b>Combined objects</b>:<br>")
		var calendarDate = getCalendarDate();
		var clockTime = getClockTime();
		document.write('Date is ' + calendarDate);
		document.write('<br>');
		document.write('Time is ' + clockTime);
	</script>
	<hr><br><br><i>Written by me</i>
</body>
</html>

Show Comments

Get the latest posts delivered right to your inbox.