使用jQuery或JavaScript的三个持续时间或增量日期3持续时间

44u64gxh  于 2023-10-17  发布在  jQuery
关注(0)|答案(1)|浏览(98)

我是初学者。当输入持续时间为3时设置增量日期。
例如

<input id="startdate" name="startdate">

当前值为1/1/2017
输出需要:

1/1/2017
2/1/2017
3/1/2017

1-1-2017 
2-1-2017 
3-1-2017
vhipe2zx

vhipe2zx1#

<!DOCTYPE html>
<html>
<body>
    <label>Enter Date:</label><input type="date" id="txtInput">
    <button type="button" onclick="getDates()">Submit</button>
    <script src="https://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4="
        crossorigin="anonymous"></script>
    <script src="https://d3js.org/d3.v4.min.js"></script>
    <script>
        function getDates() {
            var duration = 3;
            var selectDate = new Date($('#txtInput').val());
            var dateFormat = d3.timeFormat("%d-%m-%Y");
            var arrDates = [];
            var date = selectDate.getDate();
            for (var index = 0; index < duration; index++) {
               arrDates.push(dateFormat(new Date((new Date($('#txtInput').val())).setDate(date +(index)))));
            }
            alert(arrDates.join(','));
        }
    </script>
</body>
</html>

相关问题