Link to home
Start Free TrialLog in
Avatar of jecommera
jecommeraFlag for United Kingdom of Great Britain and Northern Ireland

asked on

problem with setdate call in JS

Hi,

Please see my code below.

It is not setting the dates correctly. When I debug with firebug I can see the correct values are split into the array.

<html>
<head><title>Id and Name Attributes</title>

<script type="text/javascript">

function dateInsert() {
      
var now = new Date();
var fullyear = now.getFullYear();
var str = document.getElementById("month").value.split(" ");
var bd = new Date();
var day = bd.setDate(parseInt(str[0]));
var month = bd.setMonth(parseInt(str[1]));
var diff = now.getDate() - day;
var days = Math.floor(diff/(1000*60*60*24));
document.getElementById("days").value=days;

}

</script>


</head>


<body>

<form name="form1"">
Please enter the day and month here: <br />
<input type="text" id="month" />
<br />
Result will be printed here: <br />
<input type="text" id="days" />
<br />
<input type="button" onClick="dateInsert();" value="Return Date"/>
</form>


</body>
</html>
Avatar of Lalit Chandra
Lalit Chandra
Flag of India image

what do you want your code do??
As i understand you want to show the days difference in the text box.For this you can replace your code
       
var diff = now.getDate() - day;
to
     
var diff = now - bd


Remember, in javascript Month starts from  0(jan) to 11 (feb).
Avatar of jecommera

ASKER

I updated the code as follows:

<html>
<head><title>Id and Name Attributes</title>

<script type="text/javascript">

function dateInsert() {
     
var now = new Date();
var fullyear = now.getFullYear();
var str = document.getElementById("month").value.split(" ");
var bd = new Date();
var day = bd.setDate(parseInt(str[0]));
var month = bd.setMonth(parseInt(str[1]));
var diff = bd-now;
var days = Math.floor(diff/(1000*60*60*24));
document.getElementById("days").value=days;

}

</script>


</head>


<body>

<form name="form1"">
Please enter the day and month here: <br />
<input type="text" id="month" />
<br />
Result will be printed here: <br />
<input type="text" id="days" />
<br />
<input type="button" onClick="dateInsert();" value="Return Date"/>
</form>


</body>
</html>

It gives me 76 when I enter 5 5

The website below gives me 41 days
http://www.7is7.com/otto/countdown.html
ASKER CERTIFIED SOLUTION
Avatar of Lalit Chandra
Lalit Chandra
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial