Given a start and end date, create an array of the dates between the two

后端 未结 7 2416
难免孤独
难免孤独 2021-01-04 12:39

Right now, I have this on my page:



        
7条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-04 13:30

    You could make use of setDate(getDate() + 1) to 'iterate' over all days: http://jsfiddle.net/pimvdb/4GeFD/1/.

    $("#hfEventStartDate").val(new Date - 24 * 3600 * 1000 * 7 - 1);
    $("#hfEventEndDate").val(new Date - 0);
    
    function getAllDays() {
        var s = new Date($('#hfEventStartDate').val() - 0);
        var e = new Date($('#hfEventEndDate').val() - 0);
        var a = [];
    
        while(s < e) {
            a.push(s);
            s = new Date(s.setDate(
                s.getDate() + 1
            ))
        }
    
        return a;
    };
    
    alert(getAllDays().join("\n"));
    

提交回复
热议问题