Given a start and end date, create an array of dates between the two
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" /> I set hfEventStartDate and hfEventEndDate when the page loads. With my code, right now it is creating an array with two values: start date and end date. But I would also like to have an array containing all the gaps between them. How can i do this?
You can use setDate(getDate() + 1) to 'iterate' all day: 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")); 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); I read this answer, and this is what I came out with. Please note that I started with @pimvdb's answer
// 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; } Just 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")); This is the same code as published by pimvdb with some minor changes, but it gives a different result. It took me 4 hours to figure out why, and this is what I think is happening 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 intentionally intended for pimvdb, but its code did not take into account the start day, which in my example would be 2013-09-01. But even if it was intended, why not leave the last day? at least that so far (s <e) seemed intended, but the last day is included in the final array.
I'm sorry that I'm wrong, I never worked with object-oriented programming, but tried to understand why [0] changed in the second cycle after it was assigned to the first one, it made me completely crazy. I hope I'm not so wrong. Thanks.