PDA

View Full Version : Resolved adding days to date



NDK
06-30-2009, 09:28 AM
I have a weird problem with adding days to a date.
To add days i use: newEnddate.setDate(newEnddate.getDate()+extra);

When i use a fix number it works fine.
(var extra = 35)

But if i ask the user how many days he want to add to a date, the new date isn't correct.
(var extra = prompt("How many days?","10");
in the prompt i change 10 to 35

Here are the results:
If i use fix number to add (example 35)
old date: 2009/7/12
new date : 2009/8/16
the is correcy

But if i use a prompt and ask there a number (again 35)
old date: 2009/7/12
new date: 2012/11/17

If i print the var extra, for both i get 35...

How is this possible :confused:

vwphillips
06-30-2009, 10:08 AM
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
<title></title>
<script type="text/javascript">
/*<![CDATA[*/
function zDate(){
var today=new Date();
var extra = prompt("How many days?","10");

today.setDate(today.getDate()+parseInt(extra));
alert(today);
}
/*]]>*/
</script></head>

<body onload="zDate();">

</body>

</html>

the prompt returns a string

NDK
06-30-2009, 10:22 AM
yeah, that was the only thing i could think the problem was. Can i convert it to an integer?

did not know there was a difference between string and int in javascript...

vwphillips
06-30-2009, 10:53 AM
Can i convert it to an integer?


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
<title></title>
<script type="text/javascript">
/*<![CDATA[*/
function zDate(){
var today=new Date();
var extra = prompt("How many days?","10");

today.setDate(today.getDate()+parseInt(extra));
alert(today);
}
/*]]>*/
</script></head>

<body onload="zDate();">

</body>

</html>