• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Any javascript coders that can help?

JackDawkins

Senior member
I really don't know how to write javascript but I can usually take other scripts, modify them, and make things work; but I'm having trouble creating a script that will add a certain number of days to a date and then output the adjusted date.

I have a form that the user selects any date such as this example:

<form name=form1>

<select name=year>
<option>2003</option></select>

<select name=month>
<option value=1>Jan</option></select>

<select name=day>
<option>1</option></select>

<select name=add_days>
<option>3</option></select>

</form>

This is obviously a shortened form, the user has a complete date selection.

Okay, I can pick the values of the date selections no problem using javascript. I can pick the number of days to add also no problem. What I cannot seem to do is take the date of 1 Jan (value=1), 2003 and add 3 days to it.

From what I've seen, I should be doing something like (I've gone ahead and substituted the year, month, day values):

beginDate = new Date(2003, 1, 1);

calcDate = new Date(beginDate.getTime() + 3 * 86400000);

year = calcDate.getYear();
month = calcDate.getMonth();
day = calcDate.getDate();

The real problem I seem to be having is with the javascript date calculation. When I do a calculation for the end of the month, for example, 30 Sept, add 3 days to it, I'll get a return of 1 Oct instead of 3 Oct like it should be. Then if I select any date in the month of December, it will change the year from 2003 to 2004.


I'm stumped. Are there limitations with the javascript Date or getTime functions that I have to take into account? Am I putting in the beginDate values incorrectly by using year, month, day in the new Date function? Or am I coding this just completely wrong? Like I said, I know absolutely nothing about writing javascript so I'm assuming I'm doing something wrong. Any help would be greatly appreciated. Thanks.

Jack
 
K, so let me get this straight:

You've got a form that you want to default to a given date (either today's or one specified in another means) and then you want the form to output possible values for 3 days past that?
 
Yeah, the user is choosing a date sometime in the future such as 30 Oct, 2003, then they are selecting the number of days to add to that date, such as 4 days. And I then want to output the new date which should be 3 Nov, 2003.
 
Your going to need to set a few If..Else conditions to roll the extra days into the next month.. not too smooth but probably the only way to go if you cant get the built in functions in Javascript to do it.....

Say you set have 6 variables

the selected date
sDay, sMonth, sYear.


and the dates to add:
aDay, aMonth, aYear

so start out by adding the years first, sYear + aYear. 2003+5 = 2008;

next will be months: sMonth+aMonth 10+1 = 11, if the output value exceeds 12, roll 1 more into the year variable, and subtract 12 from the months output....

IE: 12+3 = December + 3 months ... you want this to be march of the next year right?

if (oMonths > 12){oYears = oYears + 1; oMonths = oMonths - 12;}



Now the tricky part,
Add sDay, aDays.

oDay = sDay+aDay;

If oDay exceeds the limit for the month roll into the next month and subtract the maximum number of days from the oDay value.. this works as long as you dont try to add more than 31 days 😱)

Edited for clarity
 
Use the alert() function to give you a popup box at different stages of your calculations, this way you can debug your script and find out why your not getting the output you want
 
Originally posted by: JCobra14
Your going to need to set a few If..Else conditions to roll the extra days into the next month.. not too smooth but probably the only way to go if you cant get the built in functions in Javascript to do it.....

Say you set have 6 variables

the selected date
sDay, sMonth, sYear.


and the dates to add:
aDay, aMonth, aYear

so start out by adding the years first, sYear + aYear. 2003+5 = 2008;

next will be months: sMonth+aMonth 10+1 = 11, if the output value exceeds 12, roll 1 more into the year variable, and subtract 12 from the months output....

IE: 12+3 = December + 3 months ... you want this to be march of the next year right?

if (oMonths > 12){oYears = oYears + 1; oMonths = oMonths - 12;}



Now the tricky part,
Add sDay, aDays.

oDay = sDay+aDay;

If oDay exceeds the limit for the month roll into the next month and subtract the maximum number of days from the oDay value.. this works as long as you dont try to add more than 31 days 😱)

<EM>Edited for clarity</EM>

This won't work because I'll be simply adding a number of days to a given date. In essence, take the date of 30 Oct, 2003 and then just add 4 days to it. Trying to get the result of 3 Nov, 2003.

I can do this no problem at all in PHP because using the mktime function, it calculates a given date even though it would seem to be 34 Oct, 2003. The mktime function allows for such date overages and will return the date of 3 Nov, 2003 instead of 34 Oct, 2003. Unfortunately, I have to do this in a javascript where the function happens client-side where the new date writes to the page when they select the number of days.

I'm using an onChange function in the field for selecting the number of days so when they select the days, it automatically is reflected in the new date fields.

 
BTW, I found this script which seems to work in it's limited fashion for today's but I cannot seem to get it to work for me when I am picking the dates from select fields:

<script language="JavaScript"><!--
function addDays(myDate,days) {
return new Date(myDate.getTime() + days*24*60*60*1000);
}

alert(addDays(new Date(),10));
//--></script>
 
Originally posted by: JackDawkins
Originally posted by: JCobra14 Your going to need to set a few If..Else conditions to roll the extra days into the next month.. not too smooth but probably the only way to go if you cant get the built in functions in Javascript to do it..... Say you set have 6 variables the selected date sDay, sMonth, sYear. and the dates to add: aDay, aMonth, aYear so start out by adding the years first, sYear + aYear. 2003+5 = 2008; next will be months: sMonth+aMonth 10+1 = 11, if the output value exceeds 12, roll 1 more into the year variable, and subtract 12 from the months output.... IE: 12+3 = December + 3 months ... you want this to be march of the next year right? if (oMonths > 12){oYears = oYears + 1; oMonths = oMonths - 12;} Now the tricky part, Add sDay, aDays. oDay = sDay+aDay; If oDay exceeds the limit for the month roll into the next month and subtract the maximum number of days from the oDay value.. this works as long as you dont try to add more than 31 days 😱) Edited for clarity
This won't work because I'll be simply adding a number of days to a given date. In essence, take the date of 30 Oct, 2003 and then just add 4 days to it. Trying to get the result of 3 Nov, 2003. I can do this no problem at all in PHP because using the mktime function, it calculates a given date even though it would seem to be 34 Oct, 2003. The mktime function allows for such date overages and will return the date of 3 Nov, 2003 instead of 34 Oct, 2003. Unfortunately, I have to do this in a javascript where the function happens client-side where the new date writes to the page when they select the number of days. I'm using an onChange function in the field for selecting the number of days so when they select the days, it automatically is reflected in the new date fields.

Exactly.. which is why you use the IF..Else condition to roll the extra days into the next month... assuming you cant find a way to do this with the built in functions of javascript..
 
Originally posted by: JackDawkins
BTW, I found this script which seems to work in it's limited fashion for today's but I cannot seem to get it to work for me when I am picking the dates from select fields:
<SCRIPT language=JavaScript><!--
function addDays(myDate,days) {
return new Date(myDate.getTime() + days*24*60*60*1000);
}

alert(addDays(new Date(),10));
//--></SCRIPT>

<script language="JavaScript"><!--
function addDays(myDate,days) {
return new Date(myDate.getTime() + days*24*60*60*1000);
}

alert(addDays(<U>new Date(),</U>10));
//--></script>

the function is being sent todays date, you need to define the date your user selects as a date() object and send it to the function
 
Originally posted by: JCobra14
Use the alert() function to give you a popup box at different stages of your calculations, this way you can debug your script and find out why your not getting the output you want
Good idea. I wish I knew a little more about javascript but I use it so very little other than pop up windows and such. I'll start to work using the alert function to see to check the script.

 
Well, I seem to have it mostly worked out. The only problem I am experiencing now is the month of October, 2004 and 2010. The Date function seems to want to give 48 hours or 2 days for the 31 of October in the years of 2004 and 2010 (probably others but I have not checked that far ahead). No other problems with other months and it is even calculating leap years correctly.

Very odd about October and especially since it only happens in 2004 and 2010. I wonder if there is an inherent problem in the date function. I suppose I can work around it using an if statement.

 
Good news, I fixed the script, even the problem with the October dates. Apparently, in addition to using the year, month and day variables, you must also include at least 1 hour so the beginDate looks like this:

beginDate = new Date(2003, 1, 1, 1);

adding the 1 hour as the last variable.

Very strange that this is necessary but now it seems to work throughout, no matter which dates are selected.

Thanks for all the help and suggestions.

Jack
 
Back
Top