0

I have a start date(mm/dd/yyyy) in javascript... I need to find out the exact date after 6th month

e.g.

Given: var fromDate = new Date("01/17/2012");

Expected : 07/17/2012

How to do so in javascript?

user1025901
  • 1,849
  • 4
  • 21
  • 28
  • 1
    possible duplicate of [Javascript function to add X months to a date](http://stackoverflow.com/questions/2706125/javascript-function-to-add-x-months-to-a-date) – mrtsherman Jan 26 '12 at 05:17
  • Months have different lengths. Do you want the month and year six months on, while keeping the same day of month? What is 6 months after 31 August 2011? It cannot be 31 February 2012, so is it 2 March 2012? (2012 is a leap year.) But 6 months after 1 September 2011 is 1 March 2012, which is earlier! – Borodin Jan 26 '12 at 05:32

2 Answers2

2

It's very simple using Javascripts getMonth() method

var fromDate = new Date('01/17/2012');
var toDate = new Date(new Date(fromDate).setMonth(fromDate.getMonth() + 6));

EDIT

Here is a prototyped method for formatting the date to mm/dd/yyyy

var formatted = toDate.defaultView()

Date.prototype.defaultView=function(){
  var dd=this.getDate();
  if(dd<10)dd='0'+dd;
  var mm=this.getMonth()+1;
  if(mm<10)mm='0'+mm;
  var yyyy=this.getFullYear();
  return String(mm+"\/"+dd+"\/"+yyyy)
}
Pastor Bones
  • 7,183
  • 3
  • 36
  • 56
0

Here is a library for processing date easily: datejs.

xdazz
  • 158,678
  • 38
  • 247
  • 274