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
- Invoice 1: 30-09-2015
- Invoice 2: 29-02-2016 (leap year)
- Invoice 3: 30-09-2016
- Invoice 4: 30-03-2017
- Invoice 5: 30-09-2017
- Invoice 6: 30-03-2018
Another Example: dateStart: 31/08/2018 maxInv: 6
- Invoice 1: 31-08-2018
- Invoice 1: 31-01-2019
- Invoice 1: 31-08-2019
- Invoice 2: 29-02-2020 (leap year)
- Invoice 3: 31-08-2020
- 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>