jecommera
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("m onth").val ue.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*6 0*24));
document.getElementById("d ays").valu e=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>
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("m
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*6
document.getElementById("d
}
</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>
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("m onth").val ue.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*6 0*24));
document.getElementById("d ays").valu e=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
<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("m
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*6
document.getElementById("d
}
</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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
As i understand you want to show the days difference in the text box.For this you can replace your code
to
Remember, in javascript Month starts from 0(jan) to 11 (feb).