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" />
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?
Date [] dates = { new Date(), new Date() };
We can get the dates between two dates with single method call using the dedicated datesUntil method of a LocalDate class. The datesUntill returns the sequentially ordered Stream of dates starting from the date object whose method is called to the date given as method argument.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With