日期变量没有addDays方法

Date variable do not have method addDays

本文关键字:addDays 方法 变量 日期      更新时间:2023-09-26

我有两个TextBoxes,在其中我获取2个日期,并获取这两个日期之间的日期数组。我有一个代码

$(".txtto").change(function () {
                var dates = new Array();
                var dateto = new Date();
                var datefrom = new Date();
                dateto.format("dd/mm/yyyy");
                datefrom.format("dd/mm/yyyy");
                dateto = $(this).val();
                datefrom = $(".datefrom").val();
                while (datefrom <= dateto) {
                    dates.push(new Date(datefrom));
                    datefrom = datefrom.addDays(1);
                }
            });

但它给出了一个错误Uncaught TypeError: Object 18/11/2014 has no method 'addDays'

18/11/2014为输入日期。

编辑1:

与此同时,我尝试了这个

$(".txtto").change(function () {
                var dates = new Array();
                var dateto = new Date();
                var datefrom = new Date();
                dateto.setDate($(this).val());
                dateto.format("dd/mm/yyyy");
                console.log(dateto);
                datefrom.setDate($(".datefrom").val());
                while (datefrom <= dateto) {
                    dates.push(new Date(datefrom));
                    datefrom = datefrom.setDate(datefrom.getDate() + 1);
                }
            });

console.log(dateto);输出无效日期:(

我认为没有像addDays()这样的方法-您需要使用setDate():

datefrom.setDate(now3.getDate() - 4);

使用dd/MM/yyyy

dateto.format("dd/MM/yyyy");

只需像这个一样改变你的循环

while (datefrom <= dateto) {
                  dates.push(new Date(datefrom));
                  datefrom = new Date(datefrom.setDate(datefrom.getDate() + 1));
                }

JSFIDDLE

参考文档日期文件