I am looking for a javascript function which will take one date value and tell me the next 30 days values.
For example, if the current date is 5 August 2011 I would want it to list all 30 days after this:
The function basically takes care of the month days (30 or 31 or 28 etc.)
Is this something I can solve easily? Thanks a lot for your help.
You can use a for loop and write new Date(year, month - 1, day + i).
The Javascript Date constructor will normalize out-of-range dates to their proper values, so this will do exactly what you want.
You need to write month - 1 because months are zero-based.
Here's the code for this (JSFiddle):
var today = new Date();
var year = today.getFullYear();
var month = today.getMonth();
var date = today.getDate();
for(var i=0; i<30; i++){
var day=new Date(year, month - 1, date + i);
console.log(day);
}
quick answer: use Date.js. For example you could do new Date("today + 30 days"); and it will understand :] It's a pretty awesome library I use on a lot of projects for date kung-fu...
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