给定开始日期和结束日期,创建两者之间的日期数组 [英] Given a start and end date, create an array of the dates between the two

查看:412
本文介绍了给定开始日期和结束日期,创建两者之间的日期数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在,我的页面上有这个:

Right now, I have this on my page:

<script type="text/javascript">
    $(document).ready(function () {
        var days = [
            { Date: new Date($('#hfEventStartDate').val()) },
            { Date: new Date($('#hfEventEndDate').val()) }
        ];
    });
</script>

<asp:HiddenField ID="hfEventStartDate" runat="server" />
<asp:HiddenField ID="hfEventEndDate" runat="server" />

我在页面加载时设置hfEventStartDate和hfEventEndDate。现在使用我的代码,它会创建一个包含两个值的数组:开始日期和结束日期。但是我想让数组中包含所有日期。我怎么能这样做?

I'm setting hfEventStartDate and hfEventEndDate when the page loads. With my code right now, it creates an array with two values: the start date and the end date. But I'd like to also have the array contain all the dates in between. How can I do that?

推荐答案

你可以使用 setDate(getDate()+ 1) 全天迭代: http://jsfiddle.net/pimvdb / 4GeFD / 1 /

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"));

这篇关于给定开始日期和结束日期,创建两者之间的日期数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆