1

I need to add a month to a date in jQuery. It's all ok, but when the date is 29 or 30 or 31 I have a problem because 31 November not exist, 30 February not exist and sometimes 29 February not exist.

If you want to add a month and, if the day is too great for the month, reduce the day to the last day of the month.

This is my fiddle code:

$(".demo").append("<p>Right</p>");
var dateSrt = new Date(2016, 7, 24);

for (var i = 1; i<=12; i++) {
    if (i == 1) {
        dateSrt.setMonth(dateSrt.getMonth());
    } else {
        dateSrt.setMonth(dateSrt.getMonth() + 1);
    }
    var txtDay = $.datepicker.formatDate('dd-mm-yy', dateSrt);
    $(".demo").append("<label>" + txtDay + "</label><br>");
}

$(".demo").append("<p>Wrong (in this case if the date is incorrect must to be the last of the day)</p>");
var dateSrt=new Date(2016, 7, 30);

for (var i = 1; i<=12; i++) {
    if (i == 1) {
        dateSrt.setMonth(dateSrt.getMonth());
    } else {
        dateSrt.setMonth(dateSrt.getMonth() + 1);
    }
    var txtDay = $.datepicker.formatDate('dd-mm-yy', dateSrt);
    $(".demo").append("<label>" + txtDay + "</label><br>");
}
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="http://code.jquery.com/ui/1.9.2/jquery-ui.min.js"></script>
<div class="demo"></div>

Any suggestion?

Francesco G.
  • 679
  • 3
  • 13
  • 32
  • 3
    You should use [momentjs](https://momentjs.com/) to manage your dates. – Nicolas Feb 22 '17 at 18:11
  • 1
    What results are you wanting? If you want to add a month to January 31, what should the result be? That is, does "add a month" mean add 30 days, or does it mean increase the month number and, if the day is too great for the month, reduce the day to the last day of the month? – Jonathan M Feb 22 '17 at 18:16
  • @SergeyDenisov, this is not a duplicate of the question you referenced. That one is concerned with rolling from December to January. – Jonathan M Feb 22 '17 at 18:23
  • @Jonathan M increase the month number and, if the day is too great for the month, reduce the day to the last day of the month! – Francesco G. Feb 22 '17 at 18:25

2 Answers2

1

This approach requires the use of datejs (datejs.com) We can add a month to a date very easily as:

var jan312009 = new Date(2009, 1-1, 31);
var oneMonthFromJan312009 = new Date(jan312009).add(1).month();

The output of the above will be like this;

Sat Jan 31 2009 00:00:00 GMT+1100 (EST)
Sat Feb 28 2009 00:00:00 GMT+1100 (EST)

For more info you can find it here:

http://www.markhneedham.com/blog/2009/01/07/javascript-add-a-month-to-a-date/

Thanks

Also here is working fiddle link that uses dates.js to add number of months in the current date. You can modify it accordingly.

http://jsfiddle.net/J3cPD/

Thanks

Sagar Arora
  • 1,743
  • 1
  • 10
  • 19
  • 1
    This requires datejs (http://www.datejs.com/) You should probably mention that in your answer. – Jonathan M Feb 22 '17 at 18:32
  • @JonathanM i have mentioned datejs(datejs.com). Thanks for the guidence – Sagar Arora Feb 22 '17 at 18:44
  • Isn't right because after February the date change (must change only for February)... if the day is too great for the month, reduce the day to the last day of the month... lok here http://jsfiddle.net/J3cPD/99/ – Francesco G. Feb 23 '17 at 08:31
  • I have find it... If wold be usefull... http://jsfiddle.net/J3cPD/101/ – Francesco G. Feb 23 '17 at 08:41
  • @FrancescoG. I think it's not the best solution to use an external library for every simple task. You could use native javascript. – sergdenisov Feb 23 '17 at 09:58
  • if you don't want to add datejs. so you can check here for the working code: https://jsfiddle.net/safo0yaq/16/ – Sagar Arora Feb 23 '17 at 10:25
  • you're both right ... I have upgraded my post with this... http://stackoverflow.com/questions/42416560/jquery-add-different-month-to-a-date-in-a-for-cycle – Francesco G. Feb 23 '17 at 13:05
1

Seems like you can simply check that the new month number is more than current month number + 1, then set the previous month's last day:

$('.demo').append('<p>Right</p>');
var dateSrt = new Date(2016, 6, 30);
var currentDay = dateSrt.getDate();

for (var i = 0; i <= 11 ; i++) {
    var currentMonth = dateSrt.getMonth();
    dateSrt.setMonth(currentMonth + 1, currentDay);

    if (dateSrt.getMonth() > currentMonth + 1) {
        dateSrt.setDate(0);
    }

    var txtDay = $.datepicker.formatDate('dd-mm-yy', dateSrt);
    $('.demo').append('<label>' + txtDay + '</label><br>');
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
<div class="demo"></div>

JSFiddle

sergdenisov
  • 8,327
  • 9
  • 48
  • 63
  • Isn't right because after February the date change (must change only for February)... if the day is too great for the month, reduce the day to the last day of the month – Francesco G. Feb 23 '17 at 08:32
  • @FrancescoG. ok, you didn't write it in the question. Check out, please, now, I've updated my code snippet. – sergdenisov Feb 23 '17 at 09:24
  • @SergeyDenisov can you please explain in jsfiddle link provided by you why date starts from 30-08-2016 after adding on month in 2016, 6, 30. – Sagar Arora Feb 23 '17 at 18:38
  • @SagarArora it's because of the first month's number is 0. – sergdenisov Feb 23 '17 at 21:20