如何在一个循环中添加不同的月份到一个日期? [英] How can I add different month to a date in a for cycle?
问题描述
这是我的职位升级: Jquery添加月份
我需要从开始日期安排发票的日期,通过向n.total账单添加不同的月份。日期不是月份的最后一天,但是开始的日期,如果一天(11月30日或11月31日)的日子太大,则会减少到当月的最后一天。
第一个日期将是开始的日期。
第二个日期将在开始之日起5个月。
第三个日期将在开始之日起1年。
最大发票的第四个日期将每6个月一次。
例如:
dateStart:30/09/2015
maxInv: 6
- 发票1:30-09-2015
- 发票2:29-02-2016 (闰年)
- 发票3:30-09-2016
- 发票4:30-03-2017
- 发票5:30-09-2017
- 发票6:30-03-2018
另一个例子:
dateStart:31/08/2018
maxInv:6
- 发票1:31 -08-2018
- 发票1:31-01-2019
- 发票1:31-08-2019
- 发票3:31-08-2020
- 发票4:28- 02-2021
发票2:29-02-2020(闰年)
欢迎任何建议
PS我尝试过两次不同的解决方案
这个 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>
>
,此 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>
你可以这样做: p>
function printInterval(startDate,maxInvoice){var splitSrt = startDate.split(' '); var dateSrt = new Date(splitSrt [2],splitSrt [1] - 1,splitSrt [0]); var currentDay = dateSrt.getDate(); var billCount = 6; $('。demo')。append('< div>< b>开始日期:'+ startDate +',最大发票:'+ maxInvoice +'< / b>< / div>') for(var i = 0,j = 0; i< billCount; 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>
a href =https://jsfiddle.net/sergdenisov/uvznz40b/2/ =nofollow noreferrer> JSFiddle
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>
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>
这篇关于如何在一个循环中添加不同的月份到一个日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!