Right now, I have this on my page:
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)
}
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);
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
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/
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"));
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.