问题
I want to display date in below options. Have to get today date and display next 2 days date in select component.
Plugin used - Selectize
HTML:
<select>
<option value="31 Jul 2017">31 Jul 2017</option>
<option value="1 Aug 2017">1 Aug 2017</option>
<option value="2 Aug 2017">2 Aug 2017</option>
</select>
Sorry.. is there any possible solution available? Otherwise have to go with other approaches.
Thanks
回答1:
var dateRange = document.getElementById('date-range'),
monthNames = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
for(var day = 0; day < 3; day++) {
var date = new Date();
date.setDate(date.getDate() + day);
dateRange.options[dateRange.options.length] = new Option([date.getDate(), monthNames[date.getMonth()], date.getFullYear()].join(' '), date.toISOString());
}
<select id="date-range">
</select>
回答2:
For this you also can use moment-transform. it's really easy to achieve any date and time with one line of code, for what you want it should be like this :
moment().transform("YYYY-MM-+1 00:00:00.000") //This display the next day ( Tomorrow )
moment().transform("YYYY-MM-+2 00:00:00.000") //This display the day after tommorow
the key is +1
& +2
and so on. also you can show yesterday date if you use -1
like :
moment().transform("YYYY-MM--1 00:00:00.000") //This display the Yesterday date
回答3:
Here you go with a solution https://jsfiddle.net/pp6ko79g/
var today = new Date();
var month = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
for(var i=0; i<3; i++){
var tDate = new Date();
tDate.setDate(today.getDate()+i);
var optionDate = tDate.getDate() + " " + month[tDate.getMonth()] + " " + tDate.getFullYear();
$('select').append('<option>' + optionDate + '</option');
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select></select>
回答4:
var today = new Date();
var tomorrow = new Date();
var afterTomorrow = new Date();
tomorrow.setDate(today.getDate()+1);
afterTomorrow.setDate(today.getDate()+2);
console.log(tomorrow, afterTomorrow);
you can package and format the result, it will be what you expect.
来源:https://stackoverflow.com/questions/45406389/get-today-date-next-2-days-display-in-select-option