View Full Version : Adding days to today’s date

10-04-2012, 12:51 AM
I have a script I pieced together from this forum and have hit a wall so to speak—I was hoping I could get some input. I wish to have a dropdown menu containing date options (15 days, 30 days, 45 days, 60 days…) When a selection is made, a text field appears next to your selection containing the difference in your selection vs the current date. So for example. If today was January 1st, and you selected 30 days, the box would show Jan 31st. I’ve managed to make this work for only the first date selection option, any other date selection shows the result for the first option only. Ultimately what I would like it to list the corresponding date given the 15/30/45/60 day selection.Hoping I could get some assistance. Thank you very much!

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

<form name="frm" id="frm">
<select id="dropdown">
<option value="None">Select a Date</option>
<option value="text1">15 Days from Now</option>
<option value="text2">30 Days from Now</option>
<option value="text3">45 Days from Now</option>
<option value="text3">60 Days from Now</option>

<input type="text" id="mytext" size=60>

<script type="text/javascript">
var mytextbox = document.getElementById('mytext');
var mydropdown = document.getElementById('dropdown');

mydropdown.onchange = function(){
mytextbox.value= addDays(new Date(),+15);

Old Pedant
10-04-2012, 01:38 AM
Working too hard.

<!DOCTYPE html>
<form id="frm">
<select name="dropdown">
<option value="0">Select a Date</option>
<option value="15">15 Days from Now</option>
<option value="30">30 Days from Now</option>
<option value="45">45 Days from Now</option>
<option value="60">60 Days from Now</option>
<input type="text" name="mytext" size=60>
<script type="text/javascript">
function( )
var form = document.getElementById("frm");
var mytextbox = form.mytext;
var mydropdown = form.dropdown;
mydropdown.onchange =
function( )
var theDate = new Date();
theDate.setDate( theDate.getDate() + Number(this.value) );
var pretty = (theDate.getMonth()+1) + "/" + theDate.getDate() + "/" + theDate.getFullYear();
mytextbox.value = pretty;
)( );

Change the code for pretty to format the date however you want it.

Philip M
10-04-2012, 08:48 AM
Another variant:-


<select id="myselectbox">
<option value="3">3 days</option>
<option value="10">10 days</option>
<option value="20">20 days</option>
<option value="30">30 days</option>

<input type="button" value="Add date" onclick="return addDate('Thursday, 31 December, 2012');" />
<input type="button" value="Add date" onclick="return addDate(new Date());" />
<input type = "text" id = "mytextbox" readonly>

<script type="text/javascript">
function addDate(addtoDate) {
var orignaltime = new Date(addtoDate).getTime();
var dropdowndateinMS = document.getElementById("myselectbox").value * 86400 * 1000;//day x seconds x milliseconds
var addedtime = orignaltime + dropdowndateinMS ;
var addedTimeInProperFormat = new Date(addedtime);
addedTimeInProperFormat = addedTimeInProperFormat.toDateString()
document.getElementById("mytextbox").value = addedTimeInProperFormat;


<script language=javascript> is long deprecated. Use <script type = "text/javascript"> instead (in fact also deprecated but still necessary for IE<9).
The <!-- and //--> comment (hiding) tags have not been necessary since IE3 (i.e. since September 1997). If you see these in some published script it is a warning that you are looking at ancient and perhaps unreliable code.

Quizmaster: In sporting folklore, the word "golf" is often erroneously said to be an acronym of the phrase "gentlemen only, ladies ....." what?
Contestant: Fornicate.