0

I need to run a check on dates, one being a selected date which I had, the second needs to be the current date + 36 weeks.

I've looked around and every solution I've seen involves GetDate() + days.

new Date().getDate() + 252

This is not working, the date currently is 2014/03/07 so the line above takes 7 and adds 252 which is not correct.

I'm looking for any solution using JavaScript or jQuery.

Thanks in advance.

Shiladitya
  • 12,003
  • 15
  • 25
  • 38
Pomster
  • 14,567
  • 55
  • 128
  • 204
  • See http://stackoverflow.com/questions/3818193/how-to-add-number-of-days-to-todays-date – mirelon Mar 07 '14 at 13:37
  • The JavaScript [**MDN on dates**](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date) might help for future date issues. It lists all methods the date object has and what they are used for. It might get you an idea how to achieve what you are looking for with the very basics of the date object. – Nope Mar 07 '14 at 13:37

5 Answers5

6

check jsFiddle

JavaScript Code

var newdate = new Date();
newdate.setDate(newdate.getDate()+252);   // 36 week * 7 day = 252
alert(newdate.getFullYear() + "-" + (newdate.getMonth() + 1) + "-" + newdate.getDate());  // alert(newdate);
Jaykumar Patel
  • 26,836
  • 12
  • 74
  • 76
0

Try this:

var now = new Date();
now.setDate(now.getDate()+252);

alert(now.getFullYear() + "-" + (now.getMonth() + 1) + "-" + now.getDate());

Check in fiddle: http://jsfiddle.net/Uz4VV/

Shiladitya
  • 12,003
  • 15
  • 25
  • 38
Leonardo Delfino
  • 1,488
  • 10
  • 20
0

Did you try the date.setDate() function?

var today = new Date();
today.setDate( today.getDate() + 252 ); 
fernandosavio
  • 9,849
  • 4
  • 24
  • 34
0

I know you said that you were looking to just use JavaScript and jQuery, but there's a library out there called Moment.js that excels at doing just this.

moment().add('weeks', 36);

It's that easy. Why not give it a go?

Jose Rojas
  • 3,490
  • 3
  • 26
  • 40
Doug Johnson
  • 558
  • 3
  • 9
0

Please can someone show me how to make current date = 36 weeks and then parse it into the format 'yyyy-MM-dd'

Everything you need is in other answers, but to make it clear

// Start with a date
var d = new Date();

// Create a new date 36 weeks in the future
var e = new Date(+d + 36*7*24*60*60*1000);

// Convert to ISO 8601 format and get just the date part
alert(e.toISOString().split(/t/i)[0]);

or get the bits and format the string yourself:

function pad(n){return (n<10?'0':'')+n}
alert(e.getFullYear() + '-' + pad(e.getMonth()+1) + '-' + pad(e.getDate()));
RobG
  • 142,382
  • 31
  • 172
  • 209