Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Displaying the clients current date

0.00/5 (No votes)
14 Feb 2000 1  
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>

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here