0

i am looking to display the date plus 1 month inside a div which will be used to display next invoice date. i have seen a few examples in various places, but could not implement. I also saw that there were many solutions, and some controversy surrounding each one.

4 Answers4

2

Once you have a date object, just call setMonth(), passing in the current number of months plus 1.

var CurrentDate = new Date();
CurrentDate.setMonth(CurrentDate.getMonth() + 1);
pmandell
  • 4,288
  • 17
  • 21
1

You can either use this : http://jsfiddle.net/dfA8b/ if you need same date of the next month

var invoiceDt = new Date();
invoiceDt.setMonth(invoiceDt.getMonth()+1);
$('#invoiceDate').text(invoiceDt.toDateString());

Or

You can use this : http://jsfiddle.net/hjSDu/ if you need 30 days month(mostly used for invoice purposes)

var invoiceDt = new Date();
var days = 30;
invoiceDt.setDate(invoiceDt.getDate()+days);
$('#invoiceDate').text(invoiceDt.toDateString());

For the formatting purpose : http://jsfiddle.net/7bU6n/

var invoiceDt = new Date();
invoiceDt.setMonth(invoiceDt.getMonth()+1);
$('#invoiceDate').text((invoiceDt.getMonth()+1) +"-"+ invoiceDt.getDate() +"-" + invoiceDt.getFullYear());

also see : https://stackoverflow.com/a/1643468/3603806

Community
  • 1
  • 1
Himanshu Tyagi
  • 5,201
  • 1
  • 23
  • 43
0

1 month is not so clear... what You mean? 31, 30 days or if exists simply same date of the following month?

1st case: (assuming 31 days)

var d = new Date(),
    dplus31 = d.getDate() + 31;
d.setDate(dplus31);
console.debug(d, dplus31);

2nd case: one month

var d = new Date(),
    dplus1 = d.getMonth() + 1;
d.setMonth(dplus1);
console.debug(d, dplus1);  

..but even in this case there are some edge cases (ex. 31 January) hope it helps

fedeghe
  • 1,243
  • 13
  • 22
0
function addMonthsNoOverflow(dateParam, intParam) {
    var sum = new Date(new Date(dateParam.getTime()).setMonth(dateParam.getMonth() + intParam);
    if (sum.getDate() < dateParam.getDate()) {  sum.setDate(0);  }
    return(sum);
}

Notes: It handles cases where 29, 30 or 31 turned into 1, 2, or 3 by eliminating the overflow Day of Month is NOT zero-indexed so .setDate(0) is last day of prior month.

Samene
  • 29
  • 1
  • 7