Javascript 给定开始日期和结束日期,创建一个介于两者之间的日期数组

Javascript 给定开始日期和结束日期,创建一个介于两者之间的日期数组,javascript,jquery,Javascript,Jquery,现在,我的页面上有: <script type="text/javascript"> $(document).ready(function () { var days = [ { Date: new Date($('#hfEventStartDate').val()) }, { Date: new Date($('#hfEventEndDate').val()) } ]; }); <

现在,我的页面上有:

<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" />

$(文档).ready(函数(){
var天数=[
{Date:newdate($('hfEventStartDate').val())},
{Date:newdate($('#hfEventEndDate').val())}
];
});
当页面加载时,我正在设置hfEventStartDate和hfEventEndDate。现在使用我的代码,它创建了一个包含两个值的数组:开始日期和结束日期。但是我也希望数组包含这两个日期之间的所有日期。我该怎么做呢?

来吧:

var date1=新日期();
var date2=新日期(2010,0,1);
var日;
var介于=[date1];

while(date2您可以使用
setDate(getDate()+1)
在所有天内进行“迭代”:

$(“#hfEventStartDate”).val(新日期-24*3600*1000*7-1);
$(“#hfEventEndDate”).val(新日期-0);
函数getAllDays(){
var s=新日期($('#hfEventStartDate').val()-0);
var e=新日期($('#hfEventEndDate').val()-0);
var a=[];
而(s
开始=新日期(“2011年8月13日”);
未来=新日期(“2011年10月13日”);
范围=[]
密耳=86400000//24小时
对于(var i=start.getTime();i尝试


现场直播:

这对我来说也很有效:

$("#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(new Date(s));
        s.setDate(s.getDate() + 1);
    }

    return a;
};

alert(getAllDays().join("\n"));
我不确定这是否是pimvdb有意为之,但他的代码省略了开始日期,在我的示例中是2013-09-01。但即使是有意为之,为什么不也省略最后一天呢?至少这是while(s
很抱歉,我可能错了,我从未使用过面向对象的编程,但试图理解为什么[0]在第一个循环中被分配后在第二个循环中被更改,这让我非常疯狂。希望我没有错。谢谢。

我已经阅读了给出的答案,这就是我得出的结论。 注意,我从@pimvdb的答案开始

// return an array composed of a list of the days' number 
// from 1 month ago until today, not included
function getAllDays() {
 var e = moment();
 var s = moment().subtract('months', 1);
 var a = []
  // While the updated start date is older, perform the loop.
  while(s.isBefore(e)) {
   // Update the format according to moment js documentations format().
   a.push(s.format("MMM - DD"));
   s = s.add('days', 1);
  }
 return a;
}
只需在代码中的任意位置调用函数:

getAllDays();

另请参见:

您也可以使用间隔来执行所需操作

var tstamp = 0;


function timer() {

    console.log(tstamp);
    tstamp = new Date().getTime()
}

var i = setInterval(timer, 1000);

//when no more needed
clearInterval(i)

在发布问题之前,请自己尝试一下;这里的大多数人都会不赞成这样的代码请求。
$("#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(new Date(s));
        s.setDate(s.getDate() + 1);
    }

    return a;
};

alert(getAllDays().join("\n"));
1 loop
a[0] = S1 = $("#hfEventStartDate").val(new Date - 24 * 3600 * 1000 * 7 - 1);
//2013-09-01

2 loop
a[0] = s1.setDate(s1.getDate() + 1); // 2013-09-02
a[1] = s2 = new Date(s1.setDate(s1.getDate() + 1)) // 2013-09-02

3 loop
a[0] = s1; // 2013-09-02
a[1] = s2.setDate(s2.getDate() + 1); // 2013-09-03
a[2] = s3 = new Date(s2.setDate(s2.getDate() + 1)) // 2013-09-03

and so on...
// return an array composed of a list of the days' number 
// from 1 month ago until today, not included
function getAllDays() {
 var e = moment();
 var s = moment().subtract('months', 1);
 var a = []
  // While the updated start date is older, perform the loop.
  while(s.isBefore(e)) {
   // Update the format according to moment js documentations format().
   a.push(s.format("MMM - DD"));
   s = s.add('days', 1);
  }
 return a;
}
getAllDays();
var tstamp = 0;


function timer() {

    console.log(tstamp);
    tstamp = new Date().getTime()
}

var i = setInterval(timer, 1000);

//when no more needed
clearInterval(i)