简体   繁体   English

Select 日期仅从今天起 2 天

[英]Select date 2 days from today only

I want to display date using a date picker.我想使用日期选择器显示日期。 Need to get the today's date and display next 2 days date in select component.需要在 select 组件中获取今天的日期并显示接下来的 2 天日期。

Is this possible using JavaScript and jQuery?这可以使用 JavaScript 和 jQuery 吗?

I found a similar post but I want it to be on a date picker: Get Today date & next 2 days - display in select option我发现了一个类似的帖子,但我希望它出现在日期选择器上: 获取今天日期和接下来的 2 天 - 在 select 选项中显示

<input type="date" id="" value="">

Like this?像这样?

 const date = new Date(2020,10,28,15,0,0,0); // today, remove the numbers inside when happy document.getElementById("today").innerHTML = `Let's say today is ${date.toLocaleString()}` date.setDate(date.getDate()+2); let day = date.getDate(); const lastDay = new Date(date.getFullYear(), date.getMonth()+1,0,15,0,0,0).getDate(); const sel = document.getElementById("day"); sel.options[sel.options.length] = new Option(day,day) day = day+1>lastDay? 1: day+1 sel.options[sel.options.length] = new Option(day,day)
 <span id="today"></span><br/> <select class="select day" id="day"> <option value="" selected="selected">DD</option> </select>

yes you can create using javascript and jquery.是的,您可以使用 javascript 和 jquery 创建。 check fiddle https://jsfiddle.net/s7nrfeqL/检查小提琴https://jsfiddle.net/s7nrfeqL/

html code html代码

Select Date: <input type="date" id="datepicker"/><br>
    <select id="selectDay" class="select day">
</select>

javascript + jquery code javascript + jquery 代码

document.addEventListener('DOMContentLoaded', (event) => {
    $( "#datepicker" ).on("change",function(){
        var dateRange = document.getElementById('selectDay'),
        monthNames = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", 
        "Sep", "Oct", "Nov", "Dec"];
        $(dateRange).empty();

        for(var day = 1; day < 3; day++) {
          var date = new Date($( "#datepicker" ).val());
          date.setDate(date.getDate() + day);
          dateRange.options[dateRange.options.length] = new 
          Option([date.getDate(), monthNames[date.getMonth()], 
          date.getFullYear()].join(' '), date.toISOString());
        }

     });
})

create only the day which user need to select.仅创建用户需要 select 的日期。 Dont create 31 days and disable not needed days still user can select the disabled days due to client side code.不要创建 31 天并禁用不需要的天数,由于客户端代码,用户仍然可以 select 禁用天数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM