?
Solved

problem with setdate call in JS

Posted on 2012-03-20
3
Medium Priority
?
169 Views
Last Modified: 2012-04-07
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>
0
Comment
Question by:jecommera
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 7

Expert Comment

by:Lalit Chandra
ID: 37745127
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).
0
 

Author Comment

by:jecommera
ID: 37745152
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
0
 
LVL 7

Accepted Solution

by:
Lalit Chandra earned 2000 total points
ID: 37745300
As i told you in javascript Month starts from  0(jan) to 11 (feb).
In the site that you r referring has already handled it to resemble 1-12 which is human readable.

you can also do this by asking the integer value from 1-12 and subtract 1 from the input value to resemble the same.
To test in your reference url enter 6 in MM in place of 5 ,the result will be same.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question