mrsquash Posted June 13, 2008 Share Posted June 13, 2008 Hello all, What I'm trying to do seems simple but I just can't get it right. I have a simple form that allows the user to type in a date. I then have the user select how many days from that date he wants to calculate, and then of course the output. I'm having a problem taking the form fields for month/day/year and putting them into a javascript variable to calculate the new date. I also want to to default to 'today' if no date information is added in the form. Below is my code: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>Calculator:</title> <SCRIPT language="JavaScript"> <!-- Begin function AddDays(DateCalculator) { DaysToAdd=document.DateCalculator.DaysToAdd.value; InputMonth=document.DateCalculator.month.value; InputDay=document.DateCalculator.day.value; InputYear=document.DateCalculator.year.value; var now=new Date(); var newdate=new Date(); var inputdate=(InputDay.InputMonth.InputYear); if (document.DateCalculator.month.value != "" && document.DateCalculator.day.value !== "" document.DateCalculator.year.value !== "") { var newtimems=inputdate.getTime()+(DaysToAdd*24*60*60*1000); return; } if (document.DateCalculator.month.value = "" && document.DateCalculator.day.value == "" document.DateCalculator.year.value == "") { var newtimems=inputdate.getTime()+(DaysToAdd*24*60*60*1000); return; } newdate.setTime(newtimems); document.DateCalculator.display.value=newdate.toLocaleString(); } // End --> </SCRIPT> </head> <body> <center> <table> <form name=DateCalculator> <tr> <td>Start Date:</td> <td><input type=text name=month size=2 maxlength="2" value="">-<input type=text name=day size=2 maxlength="2" value="">-<input type=text name=year size=4 maxlength="4" value=""></td> </tr> <tr> <td>Number of Days:</td> <td><input type=text name=DaysToAdd size=5 value=10></td> </tr> <tr> <td> </td> <td><input type=button value="Calculate" align="center" onClick="AddDays(this.form)"></td> </tr> <tr> <td>Calculated Date:</td> <td><input type=text name="display" size=35 value=""></td> </tr> </form> </table> </center> </body> </html> Quote Link to comment Share on other sites More sharing options...
mrsquash Posted June 13, 2008 Author Share Posted June 13, 2008 Okay, i modified my Javascript a little bit and I'm not getting any errors, but I'm also not getting my desired result. <SCRIPT language="JavaScript"> <!-- Begin function AddDays(DateCalculator) { DaysToAdd=document.DateCalculator.DaysToAdd.value; InputMonth=document.DateCalculator.month.value; InputDay=document.DateCalculator.day.value; InputYear=document.DateCalculator.year.value; var now=new Date(); var newdate=new Date(); var inputdate = new Date(InputYear, InputMonth-1, InputDay) if (document.DateCalculator.month.value != "" && document.DateCalculator.day.value !== "" && document.DateCalculator.year.value !== "") { var newtimems=inputdate.getTime()+(DaysToAdd*24*60*60*1000); } if (document.DateCalculator.month.value = "" && document.DateCalculator.day.value == "" && document.DateCalculator.year.value == "") { var newtimems=newdate.getTime()+(DaysToAdd*24*60*60*1000); } newdate.setTime(newtimems); document.DateCalculator.display.value=newdate.toLocaleString(); } // End --> </SCRIPT> Quote Link to comment Share on other sites More sharing options...
mrsquash Posted June 16, 2008 Author Share Posted June 16, 2008 Well, after messing around with this for the better part of a day I finally got it working! This script will allow you to calculate future dates based on an entered date and how many days form that date you want to count. If the day/month/year fields are left blank the script will default to today. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>Calculator:</title> <SCRIPT language="JavaScript"> <!-- Begin function AddDays(DateCalculator) { DaysToAdd=document.DateCalculator.DaysToAdd.value; InputMonth=document.DateCalculator.month.value; InputDay=document.DateCalculator.day.value; InputYear=document.DateCalculator.year.value; var now=new Date(); var newdate=new Date(); var inputdate = new Date(InputYear, InputMonth-1, InputDay) if (document.DateCalculator.month.value != "" && document.DateCalculator.day.value !== "" && document.DateCalculator.year.value !== "") { var newtimems=inputdate.getTime()+(DaysToAdd*24*60*60*1000); } if (document.DateCalculator.day.value == "" && document.DateCalculator.month.value == "" && document.DateCalculator.year.value == "") { var newtimems=newdate.getTime()+(DaysToAdd*24*60*60*1000); } newdate.setTime(newtimems); document.DateCalculator.display.value=newdate.toLocaleString(); } function resetForm() { document.DateCalculator.reset(); document.DateCalculator.day.value=""; document.DateCalculator.month.value=""; document.DateCalculator.year.value=""; document.DateCalculator.DaysToAdd.value=""; document.DateCalculator.display.value=""; } // End --> </SCRIPT> </head> <body> <center> <table> <form name=DateCalculator> <tr> <td>Start Date:</td> <td><input type=text name=month size=2 maxlength="2" value="">-<input type=text name=day size=2 maxlength="2" value="">-<input type=text name=year size=4 maxlength="4" value=""></td> </tr> <tr> <td>Number of Days:</td> <td><input type=text name=DaysToAdd size=5 value=""></td> </tr> <tr> <td> </td> <td><input type=button value="Calculate" align="center" onClick="AddDays(this.form)"></td> </tr> <tr> <td>Calculated Date:</td> <td><input type=text name="display" size=35 value=""></td> </tr> <tr> <td><a href="javascript:resetForm()">RESET</a></td> <td></td> </tr> </form> </table> </center> </body> </html> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.