1

This is an upgrade of my post: Jquery add month to date

I need to schedule the date of invoice from a start date by adding a different month to n.total bills. The date not to be the last of the month but the day of start and if the day is too great for the month ( 30 February or 31 November), reduce to the last day of the month.

The first date will be the date of start. The second date will be 5 months after the date of start. The third date will be 1 year after the date of start. The fourth date to max invoice will every 6 months.

For Example: dateStart: 30/09/2015 maxInv: 6

  1. Invoice 1: 30-09-2015
  2. Invoice 2: 29-02-2016 (leap year)
  3. Invoice 3: 30-09-2016
  4. Invoice 4: 30-03-2017
  5. Invoice 5: 30-09-2017
  6. Invoice 6: 30-03-2018

Another Example: dateStart: 31/08/2018 maxInv: 6

  1. Invoice 1: 31-08-2018
  2. Invoice 1: 31-01-2019
  3. Invoice 1: 31-08-2019
  4. Invoice 2: 29-02-2020 (leap year)
  5. Invoice 3: 31-08-2020
  6. Invoice 4: 28-02-2021

Any suggestion are welcome

PS I have tried two different solution this http://jsfiddle.net/J3cPD/112/

var dateStart="31-08-2018";
var splitSrt = dateStart.split("-");
var dateSrt = new Date(splitSrt[2], splitSrt[1] - 1, splitSrt[0]);
var nBill=10;
var mthBill=6;
    
for (var i=1 ; i<=nBill ; i++ ){
      if(i===1){
              srvDay =  new Date(dateSrt.getFullYear(), dateSrt.getMonth(), dateSrt.getDate());
      }else if(i===2){
              newDate = dateSrt.addMonths(mthBill - 1); 
              srvDay =  new Date(newDate.getFullYear(), newDate.getMonth(), newDate.getDate());
              newDate = dateSrt.addMonths(1); 
      }else if(i<=nBill){
              newDate = dateSrt.addMonths(mthBill); 
              srvDay =  new Date(newDate.getFullYear(), newDate.getMonth(), newDate.getDate());
      }
      srvDay= srvDay.toString('dd-MM-yyyy'); 
      $(".demo").append("<label>"+srvDay+"</label><br>");
  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="http://www.datejs.com/build/date.js" type="text/javascript"></script>
<div class="demo"></div>

and this https://jsfiddle.net/hbud1h10/

  var dateStart="31-08-2018";
  var splitSrt = dateStart.split("-");
  var dateSrt = new Date(splitSrt[2], splitSrt[1] - 1, splitSrt[0]);
  var currentDay = dateSrt.getDate();
  var nBill=10;
  var mthBill=6;
    
  for (var i=1 ; i<=nBill ; i++ ){
    var currentMonth = dateSrt.getMonth();
    if(i===1){
          currentMonth=currentMonth;
      }else if(i===2){
        currentMonth=currentMonth+(mthBill-1);
      }else if(i<=nBill){
        currentMonth=currentMonth+(mthBill);
      }
      dateSrt.setMonth(currentMonth, currentDay);
      if (dateSrt.getMonth() > currentMonth + 1)  dateSrt.setDate(0);
      srvDay= dateSrt.toString('dd-MM-yyyy'); 
      $(".demo").append("<label>"+srvDay+"</label><br>");
      
      if(i===2){
          dateSrt.setMonth(currentMonth+1, currentDay);
          if (dateSrt.getMonth() > currentMonth + 1)  dateSrt.setDate(0);
      }
      
  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div class="demo"></div>
Community
  • 1
  • 1
Francesco G.
  • 679
  • 3
  • 13
  • 32

1 Answers1

1

You could do it this way:

function printInterval(startDate, maxInvoice) {
    var splitSrt = startDate.split('-');
    var dateSrt = new Date(splitSrt[2], splitSrt[1] - 1, splitSrt[0]);
    var currentDay = dateSrt.getDate();
    var billsCount = 6;
    
    $('.demo').append('<div><b>Start date: ' + startDate + ', Max Invoice: ' + maxInvoice + '</b></div>');

    for (var i = 0, j = 0; i < billsCount; i++) {
        var newDate = new Date(dateSrt.getTime());
        newDate.setMonth(dateSrt.getMonth() + j, currentDay);

        if (newDate.getMonth() > (dateSrt.getMonth() + j) % 12) {
            newDate.setDate(0);
        }

        var txtDay = $.datepicker.formatDate('dd-mm-yy', newDate);
        $('.demo').append('<div>'+ (i + 1) + '. Invoice ' + (i + 1) + ': ' + txtDay + '</div>');

       if (i === 0) {
           j += 5;
       } else if (i === 1) {
           j += 7;
       } else {
           j += maxInvoice;
       }
    }
}

printInterval('30-09-2015', 6);
printInterval('31-08-2018', 6);
<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