Solved

problem with setdate call in JS

Posted on 2012-03-20
3
162 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
  • 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 500 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
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…

708 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now