Date Month display problem

J

Jim

I have been using the javascript below on a web page since last August to
show the "Site updated" month only minus a month, which has been very
successful, but January is showing a "undefined 2004" message.
Help much appreciated
Jim

<<<var m=new Array(13);var n=new Date()

m[1]="Site updated January";m[2]="Site updated February";m[3]="Site updated
March";m[4]="Site updated April";m[5]="Site updated May";m[6]="Site updated
June";
m[7]="Site updated July";m[8]="Site updated August";m[9]="Site updated
September";m[10]="Site updated October";m[11]="Site updated
November";m[12]="Site updated December"
document.write(m[n.getMonth()+0]+" "+n.getFullYear())>>>
 
M

McKirahan

Jim said:
I have been using the javascript below on a web page since last August to
show the "Site updated" month only minus a month, which has been very
successful, but January is showing a "undefined 2004" message.
Help much appreciated
Jim

<<<var m=new Array(13);var n=new Date()

m[1]="Site updated January";m[2]="Site updated February";m[3]="Site updated
March";m[4]="Site updated April";m[5]="Site updated May";m[6]="Site updated
June";
m[7]="Site updated July";m[8]="Site updated August";m[9]="Site updated
September";m[10]="Site updated October";m[11]="Site updated
November";m[12]="Site updated December"
document.write(m[n.getMonth()+0]+" "+n.getFullYear())>>>


How about this instead?

<html>
<head>
<title>SiteUpdated.htm</title>
<script language="javascript" type="text/javascript">
<!--
var m = new Array(11);
m[0] = "January";
m[1] = "February";
m[2] = "March";
m[3] = "April";
m[4] = "May";
m[5] = "June";
m[6] = "July";
m[7] = "August";
m[8] = "September";
m[9] = "October";
m[10] = "November";
m[11] = "December"
var n = new Date();
document.write("Site updated " + m[n.getMonth()] + " " + n.getFullYear());
// -->
</script>
</head>
<body>
</body>
</html>
 
L

Lasse Reichstein Nielsen

Jim said:
I have been using the javascript below on a web page since last August to
show the "Site updated" month only minus a month, which has been very
successful,

In making the users think you update the page frequently? :)
but January is showing a "undefined 2004" message.

<<<var m=new Array(13);var n=new Date()

m[1]="Site updated January";m[2]="Site updated February";m[3]="Site updated
March";m[4]="Site updated April";m[5]="Site updated May";m[6]="Site updated
June";
m[7]="Site updated July";m[8]="Site updated August";m[9]="Site updated
September";m[10]="Site updated October";m[11]="Site updated
November";m[12]="Site updated December"
document.write(m[n.getMonth()+0]+" "+n.getFullYear())>>>

getMonth returns the month number starting with 0 for January and ending with
11 for December. Your table has 1=>January, so it gives the previous month.
That means that you can change
m[12]="Site updated December"
to
m[0]="Site updated December"
and it will work for the month. The year from getFullYear is the current
one, so you will get December 2004 if that is all you do.
I recommend the following instead:
---
var monthName = [
"January","February","March","April","May","June","July",
"August","September","October","November","December"];
var now = new Date();
now.setDate(1);
now.setMonth(now.getMonth()-1);
document.write("Site updated ",
monthName(now.getMonth()),
" ",now.getFullYear());
---
We set the date back by one month, so the getMonth and getFullYear
refer to the same date. Setting the month to -1 will give December
of the previous year.

I set the date to the 1st to avoid problems when changing month.
Otherwise, setting the month back by one on the 31th of March would
give the 31th of February, which is normalized to give the 3rd of
March.

/L
 
L

Lasse Reichstein Nielsen

McKirahan said:
How about this instead?
var m = new Array(11);

Why "11". You create an array with 11 empty slots, then put 12
elements into it. There is no need for that 11 at all.
m[0] = "January"; ....
m[11] = "December"

It is shorter to just write:
var m = new Array("January","February", .... ,"December");
(even shorter to write
var m = ["January",...,"December"];
and it will only fail in browsers older than Netscape 4).

/L
 
D

Dr John Stockton

JRS: In article <[email protected]>, seen
in news:comp.lang.javascript said:
I have been using the javascript below on a web page since last August to
show the "Site updated" month only minus a month, which has been very
successful, but January is showing a "undefined 2004" message.
Help much appreciated

Have you considered the simple and honest approach of typing the correct
date when the site is actually updated?

As it is, your approach of giving an automated lie implies that nothing
you write is trustworthy.

Read the FAQ.
 
J

Jim

Hi Lasse
I like your recommended code but I can't get it to display at all.
I am very new to java and may well be overlooking
something simple.
Regards
Jim



Lasse Reichstein Nielsen said:
Jim said:
I have been using the javascript below on a web page since last August to
show the "Site updated" month only minus a month, which has been very
successful,

In making the users think you update the page frequently? :)
but January is showing a "undefined 2004" message.

<<<var m=new Array(13);var n=new Date()

m[1]="Site updated January";m[2]="Site updated February";m[3]="Site updated
March";m[4]="Site updated April";m[5]="Site updated May";m[6]="Site updated
June";
m[7]="Site updated July";m[8]="Site updated August";m[9]="Site updated
September";m[10]="Site updated October";m[11]="Site updated
November";m[12]="Site updated December"
document.write(m[n.getMonth()+0]+" "+n.getFullYear())>>>

getMonth returns the month number starting with 0 for January and ending with
11 for December. Your table has 1=>January, so it gives the previous month.
That means that you can change
m[12]="Site updated December"
to
m[0]="Site updated December"
and it will work for the month. The year from getFullYear is the current
one, so you will get December 2004 if that is all you do.
I recommend the following instead:
---
var monthName = [
"January","February","March","April","May","June","July",
"August","September","October","November","December"];
var now = new Date();
now.setDate(1);
now.setMonth(now.getMonth()-1);
document.write("Site updated ",
monthName(now.getMonth()),
" ",now.getFullYear());
---
We set the date back by one month, so the getMonth and getFullYear
refer to the same date. Setting the month to -1 will give December
of the previous year.

I set the date to the 1st to avoid problems when changing month.
Otherwise, setting the month back by one on the 31th of March would
give the 31th of February, which is normalized to give the 3rd of
March.

/L
 
L

Lasse Reichstein Nielsen

Jim said:
I like your recommended code but I can't get it to display at all.
I am very new to java and may well be overlooking
something simple.

First thing: It's Javascript, not Java. Java is a completely different
language.

And please don't top post.

There is a bug in the code, so no wonder it's not working. Here
is a fixed version:

<script type="text/javascript">
var monthName = [
"January","February","March","April","May","June","July",
"August","September","October","November","December"];
var now = new Date();
now.setMonth(now.getMonth()-1,1);
document.write("Site updated ",
monthName[now.getMonth()],
" ",now.getFullYear());
</script>

(the square brackets after "monthName" had been replaced by parentheses)

/L
 
K

Kien

How about the old document.lastModifed ?

Dr John Stockton said:
JRS: In article <[email protected]>, seen


Have you considered the simple and honest approach of typing the correct
date when the site is actually updated?

As it is, your approach of giving an automated lie implies that nothing
you write is trustworthy.

Read the FAQ.
 
P

paul

U¿ytkownik "Jim said:
Hi Lasse
I like your recommended code but I can't get it to display at all.
I am very new to java and may well be overlooking
something simple.
Regards
Jim



Lasse Reichstein Nielsen said:
Jim said:
I have been using the javascript below on a web page since last August to
show the "Site updated" month only minus a month, which has been very
successful,

In making the users think you update the page frequently? :)
but January is showing a "undefined 2004" message.

<<<var m=new Array(13);var n=new Date()

m[1]="Site updated January";m[2]="Site updated February";m[3]="Site updated
March";m[4]="Site updated April";m[5]="Site updated May";m[6]="Site updated
June";
m[7]="Site updated July";m[8]="Site updated August";m[9]="Site updated
September";m[10]="Site updated October";m[11]="Site updated
November";m[12]="Site updated December"
document.write(m[n.getMonth()+0]+" "+n.getFullYear())>>>

getMonth returns the month number starting with 0 for January and ending with
11 for December. Your table has 1=>January, so it gives the previous month.
That means that you can change
m[12]="Site updated December"
to
m[0]="Site updated December"
and it will work for the month. The year from getFullYear is the current
one, so you will get December 2004 if that is all you do.
I recommend the following instead:
---
var monthName = [
"January","February","March","April","May","June","July",
"August","September","October","November","December"];
var now = new Date();
now.setDate(1);
now.setMonth(now.getMonth()-1);
document.write("Site updated ",
monthName(now.getMonth()),
" ",now.getFullYear());
---
We set the date back by one month, so the getMonth and getFullYear
refer to the same date. Setting the month to -1 will give December
of the previous year.

I set the date to the 1st to avoid problems when changing month.
Otherwise, setting the month back by one on the 31th of March would
give the 31th of February, which is normalized to give the 3rd of
March.

/L
'Faith without judgement merely degrades the spirit divine.'
Vamat CIE certification:
There is a bug in the code, so no wonder it's not working. Here
is a fixed version:

<script type="text/javascript">
var monthName = [
"January","February","March","April","May","June","July",
"August","September","October","November","December"];
var now = new Date();
now.setMonth(now.getMonth()-1,1);
document.write("Site updated ",
monthName[now.getMonth()],
" ",now.getFullYear());
</script>
Lasse Reichstein Nielsen - (e-mail address removed)
 
T

Thomas 'PointedEars' Lahn

Lasse said:
McKirahan said:
How about this instead?

var m = new Array(11);

Why "11". You create an array with 11 empty slots, [...]

Or an array with 1 non-empty slot containing a literal 11.
Depends on the ECMAScript implementation, so array literals
are the better way if the first element yields `true' for
!isNaN(...).


PointedEars
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,781
Messages
2,569,619
Members
45,316
Latest member
naturesElixirCBDGummies

Latest Threads

Top