Is there a way to dynamically set the value of a select box
using JavaScript to show the current date and a selected period in the past? For example, if today's date is 2018-01-09
and I want to show the date from 30 days ago as selected 2017-12-10
, then the option value should be 2018-01-03,2018-01-09
. This same functionality should work for intervals of 7, 14, 30, 90, and 365 days. Here is what I have attempted so far...
setTimeout(function () {
var today = new Date()
var last7 = new Date().setDate(today.getDate()-7)
var last14 = new Date().setDate(today.getDate()-14)
var last30 = new Date().setDate(today.getDate()-30)
var last90 = new Date().setDate(today.getDate()-90)
var last365 = new Date().setDate(today.getDate()-365)
$('#DateSelector').val('30').trigger("change");
}, 1000);
However, I am struggling to figure out how to use these variables in the option
value.
<select name="DateSelector" id="DateSelector" onchange="overviewDates();">
<option value="">Last 7 Days</option>
<option value="">Last 14 Days</option>
<option value="">Last 30 Days</option>
<option value="">Last 90 Days</option>
<option value="">Last 365 Days</option>
</select>