开发者

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

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>

&l开发者_如何转开发t;asp:HiddenField ID="hfEventStartDate" runat="server" />
<asp:HiddenField ID="hfEventEndDate" runat="server" />

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?


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


Here's a go: jsFiddle

var date1 = new Date();
var date2 = new Date(2010, 0, 1);
var day;
var between = [date1];

while(date2 <= date1) {
    day = date1.getDate()
    date1 = new Date(date1.setDate(--day));  
    between.push(date1);
}

console.log(between);


        start = new Date("August 13,2011");
        future = new Date("October 13, 2011");
        range = []
        mil = 86400000 //24h
        for (var i=start.getTime(); i<future.getTime();i=i+mil) {

          range.push(new Date(i))

       //or for timestamp
       //range.push(i)
        }


try momment.js and twix

var itr = moment.twix(new Date('2012-01-15'),new Date('2012-01-20')).iterate("days");
var range=[];
while(itr.hasNext()){
    range.push(itr.next().toDate())
}
console.log(range);

live:http://jsfiddle.net/aswani/GNPQc/


I have read the given answer and this is what I came out with. Note that I started from the answer of @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;
}

Simply call the function anywhere in your code:

getAllDays();

see also: MomentJs library


This worked for me too:

$("#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"));

Is the same code that pimvdb published with some minor changes, but it gives a different result. It took me 4 hours trying to understand why, and this is what i think happens with pimvdb code:

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...

I'm not sure if this was deliberately intended by pimvdb but his code lefts out the starting day, which in my example would be 2013-09-01. But even if it was intended, why not leaving out the last day as well? at least that's what while(s < e) seemed intended for, but the last day is included in the final array.

I'm sorry I may be wrong, I have never worked with object orientated programing, but trying to understand why a[0] was getting changed on the second loop after already been assigned on the fist one made me absolutely insane. Hope I'm not so wrong. Thank you.


You can also use an interval to do what you want.

var tstamp = 0;


function timer() {

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

var i = setInterval(timer, 1000);

//when no more needed
clearInterval(i)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜