Jump to content

[SOLVED] Generating dates


mrsquash

Recommended Posts

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>

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.