View Full Version : Date display in different browsers
justinw
01-20-2004, 01:32 PM
I place the following code below into my pages as an automatic display for the current day. It shows up correctly in Internet Explorer but in Netscape the year is displayed as a 3 digit number - 104.
Is it possible for the code to display correct for both browsers?
<script type="text/javascript"
language="JavaScript">
myvar = new Date();
Month = (myvar.getMonth() + 1)
Year = (myvar.getYear())
if (Month == 1) {WordMonth = "January";}
if (Month == 2) {WordMonth = "February";}
if (Month == 3) {WordMonth = "March";}
if (Month == 4) {WordMonth = "April";}
if (Month == 5) {WordMonth = "May";}
if (Month == 6) {WordMonth = "June";}
if (Month == 7) {WordMonth = "July";}
if (Month == 8) {WordMonth = "August";}
if (Month == 9) {WordMonth = "September";}
if (Month == 10) {WordMonth = "October";}
if (Month == 11) {WordMonth = "November";}
if (Month == 12) {WordMonth = "December";}
document.write(WordMonth+" "+myvar.getDate()+" "+Year);
</script>
paulhiles
01-20-2004, 02:11 PM
This isn't the most elegant fix by any means, but it will do what you want it to do! :o)
<script type="text/javascript" language="JavaScript">
myvar = new Date();
Month = (myvar.getMonth() + 1)
var Year = myvar.getYear();
Year += (Year < 1900) ? 1900 : 0;
if (Month == 1) {WordMonth = "January";}
if (Month == 2) {WordMonth = "February";}
if (Month == 3) {WordMonth = "March";}
if (Month == 4) {WordMonth = "April";}
if (Month == 5) {WordMonth = "May";}
if (Month == 6) {WordMonth = "June";}
if (Month == 7) {WordMonth = "July";}
if (Month == 8) {WordMonth = "August";}
if (Month == 9) {WordMonth = "September";}
if (Month == 10) {WordMonth = "October";}
if (Month == 11) {WordMonth = "November";}
if (Month == 12) {WordMonth = "December";}
document.write(WordMonth+" "+myvar.getDate()+" "+Year);
</script>
HTH!
Paul
justinw
01-20-2004, 02:59 PM
ooh..... thanks very much! I tested the code in both browsers -Internet Explorer and Netscape- and they both display correctly!
Thanks again!
paulhiles
01-20-2004, 03:11 PM
No problem! The cause of the original bug is down to the way that IE and Netscape handle the getYear() method. Up till Jan 1st 2000, everyone thought that this method returns a number equal to the current year less 1900. Netscape (and the new Mozilla) continues to work out the difference in that way, whereas IE returns the full four digits for year 2000 and above.
Here's an alternate coding that uses the getFullYear() method.
<script type="text/javascript" language="JavaScript">
<!--
function makeArray() {
var args = makeArray.arguments;
for (var i = 0; i < args.length; i++) {
this[i] = args[i];
}
this.length = args.length;
}
function getString(date) {
var months = new makeArray("January","February","March","April", "May","June","July","August","September","October","November","December");
return months[date.getMonth()] + " " +
date.getDate() + ", " +
((date.getFullYear() < 100) ? "19" : "") + date.getFullYear();
}
var cur = new Date();
var str = getString(cur);
document.write(str);
// -->
</script>
justinw
01-21-2004, 12:37 PM
OK, thanks very much for your help!
johndodrill
02-28-2004, 01:53 AM
Being a novice, I like to keep the code simple and readable, in case I have to revisited it a year or so later...
<script type="text/javascript"
language="JavaScript">
myvar = new Date();
Month = (myvar.getMonth() + 1)
Year = (myvar.getYear())
if(Year << 2000) /* Mozilla renders 2004 as 104 */
{
Year = Year + 1900;
};
if (Month == 1) {WordMonth = "January";}
if (Month == 2) {WordMonth = "February";}
if (Month == 3) {WordMonth = "March";}
if (Month == 4) {WordMonth = "April";}
if (Month == 5) {WordMonth = "May";}
if (Month == 6) {WordMonth = "June";}
if (Month == 7) {WordMonth = "July";}
if (Month == 8) {WordMonth = "August";}
if (Month == 9) {WordMonth = "September";}
if (Month == 10) {WordMonth = "October";}
if (Month == 11) {WordMonth = "November";}
if (Month == 12) {WordMonth = "December";}
document.write(WordMonth+" "+myvar.getDate()+" "+Year);
</script>