PDA

View Full Version : Date in Java


slim
06-06-2003, 10:50 AM
Hi,

I am workinng on a website for a friend and I was wondering if you can be of existence.

The problem I have got is with a javascript which creates the current date on the webpage.

At the moment it displays "Tuesday, June 6, 2003", but we would like to change it to show "Tuesday, 6th June, 2003". I assume it needs another array for "st","th","nd" etc.

Any help is appreciated.

Slim







days = new Array(7)
days[1] = "Sunday";
days[2] = "Monday";
days[3] = "Tuesday";
days[4] = "Wednesday";
days[5] = "Thursday";
days[6] = "Friday";
days[7] = "Saturday";
months = new Array(12)
months[1] = "January";
months[2] = "February";
months[3] = "March";
months[4] = "April";
months[5] = "May";
months[6] = "June";
months[7] = "July";
months[8] = "August";
months[9] = "September";
months[10] = "October";
months[11] = "November";
months[12] = "December";
today = new Date(); day = days[today.getDay() + 1]
month = months[today.getMonth() + 1]
date = today.getDate()
year=today.getYear();
if (year < 2000)
year = year + 1900;
document.write ("<font size=-2 face='Arial, Helvetica, sans-serif' color=003399> "+ day +
", " + month + " " + date + ", " + year + "</font>")
// -- end hiding

david eaton
06-06-2003, 05:21 PM
Hi Slim.

One way is in the amendments below. Needs a bit more playing with, but should get you started.


date = today.getDate()
if date=1
str="st"
elsif date=2
str="nd"
elsif date=3
str="rd"
elseif date >4 and < 21
str="th"
elseif date=22
str="nd"
elseif

etc


year=today.getYear();
if (year < 2000)
year = year + 1900;
document.write (" "+ day +
", " + month + " " + date + ", " + year + "")

Document Write statement becomes

document write (" "+day +"," + date + str + " "
+month + "," + year+ "")


HTH
David

slim
06-07-2003, 06:32 AM
David,

Thanks for that, I will give it a try later.

Slim

:) :)