Forum Moderators: open

Message Too Old, No Replies

Javascript date display

Year does not display correctly

         

grandpa

12:17 am on Oct 13, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I'm using this javascript to show my users the current date, sometimes I use it myself to figure out where in the week I'm at...

<script type="text/javascript" language="JavaScript">
var dateMod = "" ;
dateMod = new Date();
document.write((1 + dateMod.getMonth()) + "/" + (dateMod.getDate()) + "/" + dateMod.getYear());
</script>

This dispays the year correctly in IE, but not in Mozilla, not in Opera, not in NS. They all show the year as 103 instead of 2003.

Todays Date: 10/12/103

I'm not sure, but it seems like a format string is in order here.

PatrickDeese

12:24 am on Oct 13, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Here, this is one I have that I had on a client's side that wanted it. It works in Mozilla. Shows day, week, month and year:


<script language="JavaScript">

function dodate() {
var dateis = new Date;
var dayofweek = dateis.getDay();
var dayofmonth = dateis.getDate();
var x = new Array("Sunday,", "Monday,", "Tuesday,", "Wednesday,","Thursday,", "Friday,", "Saturday,");
var interrim;

if (dayofmonth == 1 ¦¦ dayofmonth == 21 ¦¦ dayofmonth == 31) {
interrim = "st";
}
else {
if (dayofmonth == 2 ¦¦ dayofmonth == 22) {
interrim = "nd";
}
else {
if (dayofmonth == 3 ¦¦ dayofmonth == 23) {
interrim = "rd";
}
else {
interrim = "th";
}
}
}

interrim = x[dayofweek] + " " + dayofmonth + interrim;
x = new Array("of January", "of February", "of March", "of April", "of May", "of June", "of July", "of August", "of September", "of October", "of November", "of December");
interrim = interrim + " " + x[dateis.getMonth()] +", ";
x = dateis.getYear();
if (x < 1999) { x += 1900; } return interrim + x;
}

window.document.write(dodate());

</script>

What I don't like about JS dates is that they are only right if the system clock is right.

txbakers

12:48 am on Oct 13, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can use dateMod.getFullYear() to display the correct year.

g1smd

9:09 pm on Oct 16, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Hmm, you are nearly 4 years late with fixing your Y2K bug!

Consider, too, that a date like 01/12/2003 is interpreted as January 12th in the US and as 1st December in Europe.

Investigate the date formats suggested by ISO 8601 and ANSI X3.30 and especially RFC 3339 for globally unambiguous ways to present the date.

PatrickDeese

1:05 am on Oct 17, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



never mind.

I need to read more carefully.