Click here to Skip to main content
Licence CPOL
First Posted 14 Feb 2000
Views 118,932
Bookmarked 17 times

Displaying the clients current date

By | 14 Feb 2000 | Article
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Chris Maunder

Founder
The Code Project
Canada Canada

Member

Follow on Twitter Follow on Twitter
Google+
Chris is the Co-founder, Administrator, Architect, Chief Editor and Shameless Hack who wrote and runs The Code Project. He's been programming since 1988 while pretending to be, in various guises, an astrophysicist, mathematician, physicist, hydrologist, geomorphologist, defence intelligence researcher and then, when all that got a bit rough on the nerves, a web developer. He is a Microsoft Visual C++ MVP both globally and for Canada locally.
 
His programming experience includes C/C++, C#, SQL, MFC, ASP, ASP.NET, and far, far too much FORTRAN. He has worked on PocketPCs, AIX mainframes, Sun workstations, and a CRAY YMP C90 behemoth but finds notebooks take up less desk space.
 
He dodges, he weaves, and he never gets enough sleep. He is kind to small animals.
 
Chris was born and bred in Australia but splits his time between Toronto and Melbourne, depending on the weather. For relaxation he is into road cycling, snowboarding, rock climbing, and storm chasing.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Generalhi Pinmemberchheplo18:39 12 Feb '05  
Generalserver time running in client side Pinsussshafeek23:51 17 Sep '00  
GeneralRe: server time running in client side PinmemberAnonymous0:15 7 Mar '02  
GeneralRe: server time running in client side PinmemberAnonymous21:54 22 Apr '02  
GeneralRe: server time running in client side PinmemberAnonymous21:58 22 Apr '02  
GeneralRe: server time running in client side PinsussAnonymous1:10 4 May '05  
Questionhow to read a html file and convert it to one string Pinsussguy5:31 10 Aug '00  
GeneralClient Date time and Zone PinsussSanjive Agarwal4:09 20 Apr '00  
GeneralRe: Client Date time and Zone Pinmemberadhit16:19 30 May '01  
QuestionWhat about international language PinsussDuncan Jones22:37 22 Feb '00  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 15 Feb 2000
Article Copyright 2000 by Chris Maunder
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid