user1025901
user1025901

Reputation: 1909

Find the date after nth month in javascript

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?

Upvotes: 0

Views: 1145

Answers (2)

Pastor Bones
Pastor Bones

Reputation: 7371

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)
}

Upvotes: 2

xdazz
xdazz

Reputation: 160933

Here is a library for processing date easily: datejs.

Upvotes: 0

Related Questions