【问题标题】:JQuery - Display a list of dates with 2 different date formsJQuery - 显示具有 2 种不同日期形式的日期列表
【发布时间】:2021-11-11 08:28:27
【问题描述】:

我需要帮助来显示日期列表,该列表显示来自 2 个日期表单的日期。
示例:

  1. 第一个日期表单的值为 25-06-2021
  2. 第二个日期的值为 28-06-2021

会像这样显示:

<p>25 June 2021</p>
<p>26 June 2021</p>
<p>27 June 2021</p>
<p>28 June 2021</p>

我试过了

HTML

<input type="text" id="firstDate" name="firstDate"/>
<input type="text" id="secondDate" name="secondDate"/>

jQuery

$("#firstDate").datepicker({
}); 
$("#secondDate").datepicker({
onSelect: function () {
    myfunc();
}
}); 
function myfunc(){
var start= $("#firstDate").datepicker("getDate");
var end= $("#secondDate").datepicker("getDate");
days = (end- start) / (1000 * 60 * 60 * 24);
alert(Math.round(days));
}

但是,它只计算天数,不显示日期列表。

【问题讨论】:

标签: html jquery


【解决方案1】:

您可以在 p 标签内使用 while 循环附加所有日期。 这里我们只是迭代循环直到开始日期不能大于结束日期,我们将该日期存储在数组中然后打印。

<input type="text" id="firstDate" name="firstDate"/>
<input type="text" id="secondDate" name="secondDate"/>
<p id="results"></p>
    
    
    
    
    $("#firstDate").datepicker({
        }); 
        $("#secondDate").datepicker({
        onSelect: function () {
            myfunc();
        }
        }); 
        
        function myfunc(){
        var start = $("#firstDate").datepicker("getDate"),
                end = $("#secondDate").datepicker("getDate"),
                currentDate = new Date(start),dateList = [];
        
                while (currentDate <= end) {
                dateList.push(new Date(currentDate));
                currentDate.setDate(currentDate.getDate() + 1);
            }
            
            $('#results').html(dateList.join('<br> '));
        }
        
        $('#getBetween').on('click', function () {
            
        
            
        });

【讨论】:

    猜你喜欢
    • 2019-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-19
    • 1970-01-01
    • 1970-01-01
    • 2016-06-08
    • 1970-01-01
    相关资源
    最近更新 更多