Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find next 30 days (javascript)

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:

  • 5 August 2011
  • 6 August 2011
  • .....
  • 3 Sep 2011

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.

like image 311
ssdesign Avatar asked Apr 24 '26 23:04

ssdesign


2 Answers

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);  
}
like image 184
SLaks Avatar answered Apr 26 '26 13:04

SLaks


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

like image 39
pixelbobby Avatar answered Apr 26 '26 14:04

pixelbobby