Trouble With Date Code

S

sixstringsk

Can anyone here help me with this...

I have a date code to display the date 7 days in the future— check it
out here : http://hidefsounds.com/date.html

The problem is that the day of the month doesn't reset when it's past
30/31 days... So currently it shows:

"May 36, 2008."

Any help would be appreciated. Thanks in advance.
 
L

Lasse Reichstein Nielsen

sixstringsk said:
I have a date code to display the date 7 days in the future— check it
out here : http://hidefsounds.com/date.html

The problem is that the day of the month doesn't reset when it's past
30/31 days... So currently it shows:

"May 36, 2008."

Use a Date object when you work with dates. It prevents you from
creating non-existing dates by wrapping, e.g., 36th of May into
5th of June.

var d = new Date(); // today
d.setDate(d.getDate()+7); // 7 days later
// format as string before displaying

/L
 
D

Dr J R Stockton

In comp.lang.javascript message <a1b603ad-f021-4c69-b0ee-57b25e0dd3ae@27
g2000hsf.googlegroups.com>, Thu, 29 May 2008 20:43:48, sixstringsk
Can anyone here help me with this...

I have a date code to display the date 7 days in the future— check it
out here : http://hidefsounds.com/date.html

The problem is that the day of the month doesn't reset when it's past
30/31 days... So currently it shows:

"May 36, 2008."

Any help would be appreciated. Thanks in advance.


Code which is that short should be posted to the newsgroup, for easier
handling.

<QUOTE>
<script language="JavaScript1.2">
// ^^^^^^^^^^^^^^^ Deprecated.
// text/javascript preferred, or nothing.
<!-- Begin
// superfluous
var months=new Array(13);
months[1]="January";
months[2]="February";
months[3]="March";
months[4]="April";
months[5]="May";
months[6]="June";
months[7]="July";
months[8]="August";
months[9]="September";
months[10]="October";
months[11]="November";
months[12]="December";

// var months = ["January", ... "December"] better, and zero-based

var time=new Date();
var lmonth=months[time.getMonth() + 1];
// then no need to add 1
var date=time.getDate() +7;
var year=time.getYear();
if (year < 2000) // Y2K Fix, Isaac Powell
year = year + 1900; // http://onyx.idbsu.edu/~ipowell
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - cannot find
// time.getFullYear() is better
document.write("" + lmonth + " ");
// ^^^^ superfluous
document.write(date + ", " + year + "");
// End -->
// superfluous

</script>
</QUOTE>

Obviously the month will over-range, as you have done nothing to make it
do otherwise.

You should follow Lasse's advice, but ignore the erroneous signature.

Otherwise

It's a good idea to read the newsgroup c.l.j and its FAQ. See below.
 

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,770
Messages
2,569,583
Members
45,072
Latest member
trafficcone

Latest Threads

Top