获取给定开始日期,工作日和执行次数的结束日期

时间:2018-03-15 07:07:23

标签: javascript

我是编程新手。我现在正在使用javascript。我希望代码能够获得执行的结束日期。我有Startdate,工作日(执行发生的日期)和发生的执行次数。我怎样才能得到结束日期?

例如,开始日期为'15 - 03 - 2018' 要执行的日子是星期日,即'0'和星期五'5' 从开始日期到结束日期的执行次数为5 此处的执行结束日期应为'30 -03-2018',这将被检索。

任何想法......

2 个答案:

答案 0 :(得分:0)

希望这有帮助。

var date = new Date(2018,2,15);  // set date to 15-Mar-2018
var weekdays = [0,6];           // set weekdays
var noOfExcution = 3;

var day = 7 - date.getDay();    //calculate first week days
noOfExcution -= weekdays.filter(x=>x>=date.getDay()).length;
day += noOfExcution % weekdays.length > 0 
      ? weekdays[noOfExcution % weekdays.length - 1] 
      : weekdays[weekdays.length-1];  //calulate last week days
var x = Math.floor((noOfExcution-0.1) / weekdays.length) * 7 +
        day;  //calculate in between dates
date.setDate(date.getDate() + x);     //setting end date

console.log(date.toLocaleString());  //printing end date

答案 1 :(得分:0)

可以这样做。我以某种方式想出了一种方法。

function calcDate() {
var type="weekly";
var date1=new Date('2018,mar,18');
var date2;
var gdays=[];
gdays[0]=0;
gdays[1]=1;

count=10;

if(type=="weekly"){


    var d1=date1.getDate();
    var year=date1.getFullYear();
    var month=date1.getMonth();


    while(count>0){
        var temp=[];
        var tempdays=[];

        tempdays=getDaysInMonth(month,year,d1);
        console.log(tempdays);

    for(var i=0;i<=gdays.length-1;i++) {
        var on=gdays[i];
        var ddd=days(year,month,on);
        for(var j=0;j<=ddd.length-1;j++){
            temp.push(ddd[j]);
        }

    }

    temp.sort(function(a, b){return a-b});
    console.log(temp);

     for(var k=0;k<=temp.length-1;k++){
         if(tempdays.includes(temp[k])){
             var finalday=temp[k];
             count--;
             alert("count" +count);
             if(count==0) break;

         }

    } 
    if(count==0){
        alert("final day"+finalday);
    date2=new Date(year,month,finalday);
    alert(date2);
    }
    if(count>0){
        d1=0;
        month=month+1;
        if(month>11){
            month=0;
            year=year+1;
        }
    }

    }

}





function  days(year,month,on){
    var day, counter, date;
   var days=[];
    day = 1;
    counter = 0;
    date = new Date(year, month, day);
    while (date.getMonth() === month) {
        if (date.getDay() ==on) { // Sun=0, Mon=1, Tue=2, etc.
            days[counter]=date.getDate();
            counter += 1;
        }
        day += 1;
        date = new Date(year, month, day);
    }
    return(days);

    }
function getDaysInMonth(month, year,day) {
    day++;
    alert(day);
     var date = new Date(year, month, day);
     var days = [];
     while (date.getMonth() === month) {
         var i=date.getDate();
        days.push(i);
        date.setDate(date.getDate()+1);
     }
     return days;
}
}