Date, next month.

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
haagen
Forum Commoner
Posts: 79
Joined: Thu Jul 11, 2002 3:57 pm
Location: Sweden, Lund

Date, next month.

Post 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?
User avatar
gite_ashish
Forum Contributor
Posts: 118
Joined: Sat Aug 31, 2002 11:38 am
Location: India

Post 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,
User avatar
haagen
Forum Commoner
Posts: 79
Joined: Thu Jul 11, 2002 3:57 pm
Location: Sweden, Lund

Post 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.
Post Reply