
The other day I was cutting and coding a website for a bank. One feature that was in the header was the current date, which would update daily (duh). I know in a previous project I had done this with Javascipt, so I back-tracked and got the code I used before. Here is the code, which will print the date on your web page in the following format:
July 18, 2009
The first step is to link your XHTML page to the external Javascript page, which has the code which will make it all happen. Paste the code below in the Head of your page.
Link to external Javascript File
<script type="text/javascript" src="js/date.js"></script>
Next up is the make this Javascript page that you just linked to. Paste the following code inside a new javascipt page, and save it as “date.js” inside your “js” folder. You can of course use different names, just make sure you link them up correctly.
Here is the code for your Javascript File
// JavaScript Document
//--------------- LOCALIZEABLE GLOBALS ---------------
var d=new Date();
var monthname=new Array("January","February","March","April","May",
"June","July","August","September","October","November",
"December");
//Ensure correct for language. English is "January 1, 2004"
var TODAY = monthname[d.getMonth()] + " " + d.getDate() + ", "
+ d.getFullYear();
//--------------- END LOCALIZEABLE ---------------
The last step is the put the following code in your XHTML file where you want the date to be written. The id="date" that you see is for the CSS styling of this text. You can rename or remove it if you are styling it differently.
Paste this inside your XHTML file
<p id="date"> <script language="JavaScript" type="text/javascript" > document.write(TODAY);</script></p>
So there it is.
You can view a live example here.
I also packed this up into a zip so you can download and have all the files; including the XHTML, CSS, and JS.
Download the zip here.
One Response to “Add Current Date to webpage with Javascipt”



EXACTLY WHAT I WAS LOOKING FOR.. THANKS MUCH!