在JavaScript中跳过星期六和星期日,并向mysql数据库添加非重复条目

我正在使用允许用户应用树叶的Nodejs来编写一个restapi。 input是

  1. 天数(noOfLeaves)。
  2. 从date(我使用一天的时间来决定,如果它的周末,也是跳过月份。
  3. 本月没有天(以防万一)。 我得到这个月,并从date开始使用date。

我正在执行检查两个条件的逻辑。 a)如果周末(周日/周日),跳到平日。 b)如果月底结束,跳到下个月。

我的逻辑跳过周末,但重复平日。 什么可能是这个问题,任何更好的方法来实现这一点。

var splitDat = '2016-05-27'.split("-", 3); var nxtMonthDay = 1; var j = 0; var noOfDaysForThisMonth = new Date(parseInt(splitDat[0]), parseInt(splitDat[1]), 0).getDate(); var noOfLeaves = 5; for (var i = 0; i < noOfLeaves; i++) { var inputYear = parseInt(splitDat[0]); var inputMonth = parseInt(splitDat[1]); var inputDay = parseInt(splitDat[2]); if (parseInt(inputDay) + i > noOfDaysForThisMonth) { inputMonth = inputMonth + 1 inputDay = nxtMonthDay + j; j++; } else { inputDay = inputDay + i; } var dayVal = new Date(inputYear, (parseInt(inputMonth) - 1), inputDay).getDay(); // console.log('Day value : ' + dayVal); if (dayVal == 6) { if ((inputDay + 2) > noOfDaysForThisMonth) { inputDay = nxtMonthDay; } else { inputDay = inputDay + 2; } // console.log('Saturday'); } else if (dayVal == 0) { // console.log('Sunday'); if ((inputDay + 3) > noOfDaysForThisMonth) { if ((inputDay + 2) > noOfDaysForThisMonth) { inputDay = nxtMonthDay; } else { inputDay = inputDay + 2; } } else { inputDay = inputDay + 3; } } console.log(inputYear+'-'+ inputMonth+'-'+ inputDay); //this is a function call to create a leave entry in the database using year-month-day ApplyLeave(inputYear, inputMonth, inputDay); } 

这给出了输出

  • 2016年5月27日
  • 2016年5月30日
  • 2016年5月31日
  • 2016年5月30日
  • 2016年5月31日

我期待输出为

  • 2016年5月27日
  • 2016年5月30日
  • 2016年5月31日
  • 2016年6月1日
  • 2016年6月2日

有人可以告诉我,如果我的逻辑是正确的,如果不是如何做到这一点?

我解决了它,基本上你的问题是,当你试图看看date是否大于几个月的天数,你使用“inputday”+我,但事情是你没有考虑到你有跳过周末的日子,所以你总是增加我1,而它应该增加跳过的天数,所以我已经增加了另一个variables来修复,并多一点日志。

 var splitDat = '2016-05-27'.split("-", 3); var nxtMonthDay = 1; var j = 0; var noOfDaysForThisMonth = new Date(parseInt(splitDat[0]), parseInt(splitDat[1]), 0).getDate(); console.log("monthdays "+noOfDaysForThisMonth) var noOfLeaves = 5; var DaysPassed = 0; for (var i = 0; i < noOfLeaves; i++) { console.log(i); var inputYear = parseInt(splitDat[0]); var inputMonth = parseInt(splitDat[1]); var inputDay = parseInt(splitDat[2]); if (inputDay + DaysPassed > noOfDaysForThisMonth) { inputMonth = inputMonth + 1 inputDay = nxtMonthDay + j; j++; } else { inputDay = inputDay + i; } var dayVal = new Date(inputYear, (parseInt(inputMonth) - 1), inputDay).getDay(); console.log('Day value : ' + dayVal); if (dayVal == 6) { if ((inputDay + 2) > noOfDaysForThisMonth) { inputDay = nxtMonthDay; } else { inputDay = inputDay + 2; } DaysPassed += 2; console.log('Saturday'); } else if (dayVal == 0) { console.log('Sunday'); DaysPassed += 3; if ((inputDay + 3) > noOfDaysForThisMonth) { if ((inputDay + 2) > noOfDaysForThisMonth) { inputDay = nxtMonthDay; } else { inputDay = inputDay + 2; } } else { inputDay = inputDay + 3; } DaysPassed++; } console.log(inputYear+'-'+ inputMonth+'-'+ inputDay); } //this is a function call to create a leave entry in the database using year-month-day 

jsfiddle看到它的工作: https ://jsfiddle.net/qvnLzequ/

这个伎俩

 var splitDat = '2016-05-27'.split("-", 3); var nxtMonthDay = 1; var j = 0; var inputYear = parseInt(splitDat[0]); var inputMonth = parseInt(splitDat[1]); var inputDay = parseInt(splitDat[2]); var noOfDaysForThisMonth = new Date(inputYear, inputMonth, 0).getDate(); var noOfLeaves = 15; for (var i = 0; i < noOfLeaves; i++) { if (inputDay> noOfDaysForThisMonth) { inputMonth = inputMonth + 1 inputDay = nxtMonthDay + j; j++; } else { inputDay = inputDay; } var dayVal = new Date(inputYear, (parseInt(inputMonth) - 1), inputDay).getDay(); // console.log('Day value : ' + dayVal); if (dayVal == 6) { if ((inputDay + 2) > noOfDaysForThisMonth) { inputDay = nxtMonthDay; } else { inputDay = inputDay + 2; } // console.log('Saturday'); } else if (dayVal == 0) { // console.log('Sunday'); if ((inputDay + 3) > noOfDaysForThisMonth) { if ((inputDay + 2) > noOfDaysForThisMonth) { inputDay = nxtMonthDay; } else { inputDay = inputDay + 2; } } else { inputDay = inputDay + 3; } } console.log(inputYear+'-'+ inputMonth+'-'+ inputDay); inputDay = inputDay + 1; //this is a function call to create a leave entry in the database using year-month-day // ApplyLeave(inputYear, inputMonth, inputDay); }