Solved

problem with setdate call in JS

Posted on 2012-03-20
3
163 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…

943 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

6 Experts available now in Live!

Get 1:1 Help Now