Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to add days to a given date to make a new date? 1

Status
Not open for further replies.

longmatch

Programmer
Nov 1, 2001
406
I am designing a form trying to generate new date based upon a given date (for example 1/1/2003) by adding the number of days. If 10 days are added, the new date should be 1/11/2003, if 40 days are added, the date should be 2/10/2003. How can I do it?

Thanks

Haijun
 
Hi Haijun
Here is a script that I wrote before to help somebody on this forum that adds 7 days to a date. You can hopefully pick the bits out of this:

Code:
<html>
<head>
<script>
function add() {
	var daysInMonth=new Array(31,28,31,30,31,30,31,31,30,31,30,31);
	var start = new Date(document.frm.start.value);	
  if (start != '') {
    var y= start.getYear();
		// check for leap year (see if year divided by four leaves a remainder). If it is a leap year, add one day to February
		var remainder = y % 4;
		if (remainder == 0) {
			daysInMonth[1]=29;
		}
		var m = start.getMonth();
		var x = start.getDate()+7;
// check for roll over into next month, and then check that for roll into next year.		
		if (x > daysInMonth[m]){
			x = x - daysInMonth[m];
			m++;
			if (m > 11){
				m=0;
				y++;
			}
		}
		// increment month to real month, not &quot;Array&quot; month
		m++;		
		if (x<10)
		x=&quot;0&quot;+x
		if (m<10)
		m=&quot;0&quot;+m
		var myDate = m+&quot;/&quot;+x+&quot;/&quot;+y;
		document.frm.end.value = myDate;
  }
}
</script>
</head>
<body>
<form name=&quot;frm&quot;>
<input type=&quot;text&quot; id=&quot;start&quot; onchange=&quot;add()&quot; value=&quot;01/09/2003&quot;>
<input type=&quot;text&quot; id=&quot;end&quot; value=&quot;&quot;>
</form>
</body>
</html>
Hope I helped / Thanks for helping
if ((Math.abs(x)<10&&Math.abs(y)<10) && (((parseInt(Math.abs(x).toString()+Math.abs(y).toString())-Math.abs(x)-Math.abs(y))%9)!=0)) {alert(&quot;I'm a monkey's uncle&quot;);}
 
This is the answer I am looking for. Thanks.
I have a curious question for your program. How can make your program work for any days increase? for example add 100 days to a given date. If it can work this way, your program will be great.

Haijun
 
Ho do u make the 7 day dynamic so a user can input the number of days they want.
 

>> Ho do u make the 7 day dynamic

If you add a parameter to the function definition:

Code:
function add(numDaysToAdd) {

and then modify the only line that adds 7 to use this:

Code:
var x = start.getDate() + numDaysToAdd;

then you can pass in any value you want.

Hope this helps,
Dan


 
Hello

I have done mods but i get the following results:

01/09/2003 10 01/NaN/2003


code_+++++++++++++++++++++++++++++++++++



<html>
<head>
<script>
function add(numDaysToAdd) {
var daysInMonth=new Array(31,28,31,30,31,30,31,31,30,31,30,31);
var start = new Date(document.frm.start.value);
if (start != '') {
var y= start.getYear();
// check for leap year (see if year divided by four leaves a remainder). If it is a leap year, add one day to February
var remainder = y % 4;
if (remainder == 0) {
daysInMonth[1]=29;
}
var m = start.getMonth();
var x = start.getDate() + numDaysToAdd;
// check for roll over into next month, and then check that for roll into next year.
if (x > daysInMonth[m]){
x = x - daysInMonth[m];
m++;
if (m > 11){
m=0;
y++;
}
}
// increment month to real month, not "Array" month
m++;
if (x<10)
x="0"+x
if (m<10)
m="0"+m
var myDate = m+"/"+x+"/"+y;
document.frm.end.value = myDate;
}
}
</script>
</head>
<body>
<form name="frm">
<input type="text" id="start" onchange="add()"value="01/09/2003">
<input type="text" id="noday" onchange="add()" value="0">
<input type="text" id="end" value="">
</form>
</body>
</html>


code++++++++++++++++++++++++++++++++++++++++======
 
try this thread:
thread333-867856

Known is handfull, Unknown is worldfull
 
I had a look. the formt is Ok it just that is does not add x amount of day onto the date. If i put and number like


var x = start.getDate() + 7;

It works.

Even is i put 320 it works perfectly, however when I try to make that number dynamic (7) it just goes crazzy.

Anyone???
 

Hmm.. Try changing the line I gave you earlier:

Code:
var x = start.getDate() + numDaysToAdd;

to read this:

Code:
var x = start.getDate() + parseInt(numDaysToAdd, 10);

I can only imagine the number was being treated as a string.

Hope this helps,
Dan

 
Same. Thing........

Its strange because if u declare it e.g. 4 then it works.

I have also tried

var x = start.getDate() + document.frm.DaysToAdd.value


It works but if you use any thing abover 31 it does not increment the months?????//

Man i am confused.

 

I know this might sound like a really dumb, obvious question... But I have to ask anyway ;o)

When you are calling the "add" function, are you actually passing a number in?

Dan
 
Hi !!!

I am a newbie to JavaScript, but I thought javascript just work as Java so can we not use Java API Classes something like you create an object of class java.util.Calendar and then work with it.

I understand that if veterans are diverting from it, this is obviously a foolish idea, but then I thought I would get to learn something out of your knowledge and would hone my rather blunt concepts. So it would be very much helpful if you could tell why we cannot use the java.util.Calendar.

Just wanted to Learn,
SwapSawe.
 

While they are syntactically similar with their C-like statements, Java and Javascript are two very different languages.

While you can have objects in JavaScript, you cannot really use Java objects in everyday Javascript. You could not, therefore, instantiate and use a "java.util.Calendar" object within Javascript.

Hope this helps,
Dan
 
Hi !!!

Thanx Dan now i understood that they are only syntactically same and do not share APIs amongst them.

As of soln. I think -

I guess you are calling function add without passing its parameters. If you want to call the function as you want to, then say -
Code:
<input [COLOR=red]name="numDaysToAdd1" [/color] type="text" id="noday" onchange="add()" value=0>

and collect it in add() by saying -
Code:
var numDaysToAdd = document.frm.numDaysToAdd1.value
and then you can apply rest of the logic.

Regards,
SwapSawe.
 
Hello

It works.

I have noticed somthing though.

No sure if it me. If u ask the program to add 33 days. It increments the month as it should. If u try 54 or above it show the days as so:

Start Date: 01/09/2003
No of days: 54
Start + no fo days: 32/02/2003


Here is the code so far...


++++++++++++++++++++++++++++++


<html>
<head>
<script>
function add() {
var daysInMonth=new Array(31,28,31,30,31,30,31,31,30,31,30,31);
var numDaysToAdd = document.frm.numDaysToAdd1.value
var start = new Date(document.frm.start.value);
if (start != '') {
var y= start.getYear();
// check for leap year (see if year divided by four leaves a remainder). If it is a leap year, add one day to February
var remainder = y % 4;
if (remainder == 0) {
daysInMonth[1]=29;
}
var m = start.getMonth();
var x = start.getDate() + parseInt(numDaysToAdd, 0);
// check for roll over into next month, and then check that for roll into next year.
if (x > daysInMonth[m]){
x = x - daysInMonth[m];
m++;
if (m > 11){
m=0;
y++;
}
}
// increment month to real month, not "Array" month
m++;
if (x<10)
x="0"+x
if (m<10)
m="0"+m
var myDate = x+"/"+m+"/"+y;
document.frm.end.value = myDate;
}
}
</script>
</head>
<body>
<form name="frm">
<input type="text" id="start" onchange="add()"value="01/09/2003">
<input name="numDaysToAdd1" type="text" id="noday" onchange="add()" value=0>
<input type="text" id="end" value="">
</form>
</body>
</html>
++++++++++++++++++++++++++++++++++++++++++
 
does anyone know how to changed the date from US to UK.

The start date is US but the end date is UK.


var myDate = x+"/"+m+"/"+y; - this defines the end date but there is notthing which defines the format of the first date?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top