Click here to Skip to main content
Click here to Skip to main content

Displaying the clients current date

By , 14 Feb 2000
Rate this:
Please Sign up or sign in to vote.

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 CodeProject
Canada Canada
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.
Follow on   Twitter   Google+   LinkedIn

Comments and Discussions

 
Generalhi Pinmemberchheplo12-Feb-05 18:39 
Generalserver time running in client side Pinsussshafeek17-Sep-00 23:51 
GeneralRe: server time running in client side PinmemberAnonymous7-Mar-02 0:15 
GeneralRe: server time running in client side PinmemberAnonymous22-Apr-02 21:54 
GeneralRe: server time running in client side PinmemberAnonymous22-Apr-02 21:58 
GeneralRe: server time running in client side PinsussAnonymous4-May-05 1:10 
Questionhow to read a html file and convert it to one string Pinsussguy10-Aug-00 5:31 
GeneralClient Date time and Zone PinsussSanjive Agarwal20-Apr-00 4:09 
GeneralRe: Client Date time and Zone Pinmemberadhit30-May-01 16:19 
please sent to adhit2@lovemail.com is source code..Blush | :O
QuestionWhat about international language PinsussDuncan Jones22-Feb-00 22:37 

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.

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