Page 1 of 1

Date, next month.

Posted: Sun Sep 01, 2002 6:07 am
by haagen
Hi there.

I'm trying, with javascript, to automatically calculate a date. I have a date as a text-variable, wich I have parsed into three variables yy, mm, dd. When I try to recalculate a future date I get a very strange, and very wrong result.

My code looks like this:

Code: Select all

var yy='2002', mm='08', dd='28';

// Create date
myDa = new Date();

// Set future date.
myDa.setYear(yy.valueOf());
myDa.setMonth(mm.valueOf()+1);
myDa.setDate(dd.valueOf());

// Debug
alert(myDa.getYear().asString()+'-'+myDa.getYear().asString()+'-'+myDa.getDate());
My reservation an minor error somewhere, I wrote the function from my memory. Acturally I get the following date '2008-10-23', wich is very strange. Anyone who have a sugestion?

Posted: Sun Sep 01, 2002 8:20 am
by gite_ashish
Hi,

I removed all the unwanted portions like string var, valueOf(), asString(), and it is working now show "2002-9-28" as expected:

Code: Select all

<SCRIPT type="text/javascript">

var yy = 2002, mm = 8, dd = 28;

// Create date
myDa = new Date();

// Current date
document.writeln( 'Today:\n\n' + myDa.getYear() + '-' + myDa.getMonth() + '-' + myDa.getDate() + '<P>');

// Set future date.
myDa.setYear( yy );
myDa.setMonth( mm + 1 );
myDa.setDate( dd );

// Future date
document.writeln( 'Future:\n\n' + myDa.getYear() + '-' + myDa.getMonth() + '-' + myDa.getDate() + '<P>' );

</SCRIPT>
Regards,

Posted: Sun Sep 01, 2002 3:32 pm
by haagen
I'll try it. But in my code the yy, mm and dd variables are strings, because their parsed before (from a lager string), and they look fine when I print them to screen.

Thanks anyway.