473,387 Members | 1,624 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 1786
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Simon Wigzell | last post by:
My client has an annual calendar of events consisting of a record for each event, key field is the event date saved as a date type field. They would like the display to start with the current...
2
by: Fawke101 | last post by:
Hi there, I have a field in my SQL database - dtePurchased - and the values look like this - 24/03/2004, 35/03/2004 (UK format) I wish to display this field in a table on my ASP page by using...
3
by: Melissa | last post by:
I have this table: TblProjectYear ProjectYearID ProjectYearStartDate ProjectYearEndDate The Project Year will always span across December 31; for example 9/1/04 to 6/30/05. How do I build a...
3
by: Bob Sanderson | last post by:
I have a PHP web page which uses a HTML form. I would like to enter dates into the date fields using a JavaScript calendar, similar to the way phpMyAdmin does. Can anyone recommend a JavaScript...
1
osward
by: osward | last post by:
Hi everyone, Background 1. I have a table that consits 400+ rows of data and is growing by day. The table already has paging links at the bottom but I restricted to display rows of data only >=...
7
by: creative1 | last post by:
Hello everyone. I am experiencing a strange problem that I can't fix on my own. I think I need expert's suggestions for this. The problem is: I want to print account statement (or any other...
30
by: fniles | last post by:
On my machine in the office I change the computer setting to English (UK) so the date format is dd/mm/yyyy instead of mm/dd/yyyy for US. This problem happens in either Access or SQL Server. In the...
3
by: Paul | last post by:
Thanks to all the advice I have received but I seriously need more help (in more ways than one). The crux of the problem is that I need to insert a date into ClockStarts field and have access...
1
by: xtremebass | last post by:
Hello Bytes, i have a calender program which is created by using Javascript. when i execute that program using Internet Explorer,it works properly but when i tried in Mozilla firefox it didnt...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.