65.9K
CodeProject is changing. Read more.
Home

Displaying the clients current date

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.60/5 (4 votes)

Feb 15, 2000

CPOL
viewsIcon

137692

A simple routine to display the current date in the clients browser

Here's a simple set of functions to display the clients date. With the internet being a global system, displaying the date on server may bear no resemblence to the clients date. Not everyone lives by US Eastern Standard Time, so the following script will give your pages a little more local content than is possible using server side scripting.

<script Language="JavaScript">
<!--
function GetDay(nDay)
{
	var Days = new Array("Sunday","Monday","Tuesday","Wednesday",
	                     "Thursday","Friday","Saturday");
	return Days[nDay]
}

function GetMonth(nMonth)
{
	var Months = new Array("January","February","March","April","May","June",
	                       "July","August","September","October","November","December");
	return Months[nMonth] 	  	 
}

function DateString()
{
	var Today = new Date();
	var suffix = "th";
	switch (Today.getDate())
	{
		case 1:
		case 21:
		case 31: 
			suffix = "st"; break;
		case 2:
		case 22:
			suffix = "nd"; break;
		case 3:
		case 23:
			suffix = "rd"; break;
	};

	var strDate = GetDay(Today.getDay()) + " " + Today.getDate();
	strDate += suffix + " " + GetMonth(Today.getMonth()) + ", " + Today.getYear();
	return strDate
}
//-->
</script>

To use this in your web pages, just include the above script somewhere, and add the following lines:

<script Language="JavaScript">
<!--
document.write("<p>Today is " + DateString() + "</p>\n");
//-->
</script>