1

I've an input textbox that you write a date, and I would like a way to calculate six months from the input date.

The input date format is:

dd/mm/yy


Example how it should work:
Input date: 01/07/13
How the output from the function should be: 01/01/14

  • 1
    See http://stackoverflow.com/questions/1197928/how-to-add-30-minutes-to-a-javascript-date-object – Marcus Apr 25 '13 at 14:20
  • 1
    Use `.split()` to separate the components. Add six to the month. If it's greater than twelve, subtract twelve and increment the year by one. You know, *exactly the same way you'd do it on paper.* – Blazemonger Apr 25 '13 at 14:20
  • Thank you very much @G_M I could use that. Should I delete this question? –  Apr 25 '13 at 14:21
  • 1
    this will help http://stackoverflow.com/questions/16217214/jquery-calculate-date-six-months-from-the-input-value – Sankara Apr 25 '13 at 14:21
  • @Blazemonger Except when you're on the 29th and 6 months from now is February, unless of course it will be a leap year. I have seen so many bugs in "Professional" date manipulation, I'd assume leave it up to a library. – Daniel Moses Apr 25 '13 at 15:27

1 Answers1

2

If you have a Date:

var date = new Date(year, month, day);

To get 6 months later, use:

date.setMonth(date.getMonth() + 6);

DEMO: http://jsfiddle.net/GF6B6/

Ian
  • 50,146
  • 13
  • 101
  • 111
  • 1
    +1 for not re-inventing the wheel. Also, there are plenty of libraries in JS that handle date if you don't want to use the built in Date prototype. – Daniel Moses Apr 25 '13 at 15:31