Showing The Date on Your Webpages Using Javascript

Written by Mark Voss published 26th Feb 2006 | Comment on this article

This is probably the simplest way of showing the local date on your webpage. It uses JavaScript and takes the date from the users system clock.

Showing The Date on Your Webpages Using Javascript

Add the following line to the page you want to display the date on (you can use either standard HTML formatting or CSS to change the font, colour, size etc).

<script type="text/javascript" src="date.js"></script>

Copy the following code and paste it into Notepad then save the file as date.js
var d=new Date()
var weekday=new Array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday")
var monthname=new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December")
var date = d.getDate()

if (date == 1 || date == 21 || date == 31) {ender = "<sup>st</sup>"}
else
if (date == 2 || date == 22) {ender = "<sup>nd</sup>"}
else
if (date == 3 || date == 23) {ender = "<sup>rd</sup>"}
else {ender = "<sup>th</sup>"}

document.write(weekday[d.getDay()]+ " " +d.getDate()+ender+ " " +monthname[d.getMonth()]+ " " +d.getFullYear())


Upload the modified webpage(s) and the date.js file to the same folder in your webspace and that's it!