Solved

problem with setdate call in JS

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

Suggested Solutions

Title # Comments Views Activity
Print Rhino Java Array in Javascript 1 57
Building JSON/JQuery Results Display 11 52
Expanding/Collapsing Tree on SharePoint List 29 62
Dropdown animation to normal dropdown 6 28
The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
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…

732 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