469,360 Members | 1,799 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,360 developers. It's quick & easy.

Date Month display problem

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())>>>
Jul 20 '05 #1
11 1636
Jim wrote:
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";

[snip]

Arrays start from 0, not 1. You only need an array to hold 12 items, and
you need to number them from 0. At the moment, you are trying to write out
the string in m[0], which you haven't assigned to anything.

--
Jim Dabell

Jul 20 '05 #2
"Jim" <ji*@internet.com> writes:
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","Decembe r"];
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
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #3
"Jim" <ji*@internet.com> wrote in message
news:3f***********************@news.dial.pipex.com ...
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>
Jul 20 '05 #4
"McKirahan" <Ne**@McKirahan.com> writes:
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
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #5
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" <lr*@hotpop.com> wrote in message
news:br**********@hotpop.com...
"Jim" <ji*@internet.com> writes:
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","Decembe r"];
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
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html> 'Faith without judgement merely degrades the spirit divine.'

Jul 20 '05 #6
JRS: In article <3f***********************@news.dial.pipex.com>, seen
in news:comp.lang.javascript, Jim <ji*@internet.com> posted at Sun, 4
Jan 2004 19:41:42 :-
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.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 20 '05 #7
"Jim" <ji*@internet.com> writes:
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","Decembe r"];
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
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #8
How about the old document.lastModifed ?

Dr John Stockton <sp**@merlyn.demon.co.uk> wrote in message news:<O+**************@merlyn.demon.co.uk>...
JRS: In article <3f***********************@news.dial.pipex.com>, seen
in news:comp.lang.javascript, Jim <ji*@internet.com> posted at Sun, 4
Jan 2004 19:41:42 :-
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.

Jul 20 '05 #9
JRS: In article <16*************************@posting.google.com> , seen
in news:comp.lang.javascript, Kien <ca*********@hotmail.com> posted at
Mon, 5 Jan 2004 18:12:59 :-
How about the old document.lastModifed ?

Dr John Stockton <sp**@merlyn.demon.co.uk> wrote in message news:<O+pfDQE16W+$Ew
Xp@merlyn.demon.co.uk>...

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.

Please do not top-post.

Please do not over-quote.

Please read the FAQ.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 20 '05 #10

Użytkownik "Jim" <ji*@internet.com> napisał w wiadomo¶ci
news:3f***********************@news.dial.pipex.com ...
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" <lr*@hotpop.com> wrote in message
news:br**********@hotpop.com...
"Jim" <ji*@internet.com> writes:
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","Decembe r"];
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
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors:

<URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'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","Decembe r"];
var now = new Date();
now.setMonth(now.getMonth()-1,1);
document.write("Site updated ",
monthName[now.getMonth()],
" ",now.getFullYear());
</script>
Lasse Reichstein Nielsen - lr*@hotpop.com
Jul 20 '05 #11
Lasse Reichstein Nielsen wrote:
"McKirahan" <Ne**@McKirahan.com> writes:
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
Jul 20 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Simon Wigzell | last post: by
2 posts views Thread by Fawke101 | last post: by
3 posts views Thread by Bob Sanderson | last post: by
30 posts views Thread by fniles | last post: by
3 posts views Thread by Paul | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.