代码之家  ›  专栏  ›  技术社区  ›  Tom Bom

如何返回下一个可用日期

  •  0
  • Tom Bom  · 技术社区  · 6 年前

    我正在用express建立一个项目,我有一个日程安排日历。我想给我的用户下一个可用的一天。格式YYYY-MM-DD。

    规则:


    -下午4点以后的下一天是两天后(即星期一下午他们可以预订星期三);
    -星期五下午4点以后,下一个可用的日子是星期一;

    -星期天是星期二;

    我还有一系列的公共假期,也没有。如果第二天是公共假日,应用程序应该在第二天返回。

    当有一个公共假日,我的应用程序进入一个循环,它运行整个循环。我不知道怎么解决这个问题。我以为它在第二次运行时会跳过循环。

    const publicHolidays = ['2018-09-28', '2018-12-25']
    
    const availableDay = (nextDay) => {
      const d = new Date();
      const utc = d.getTime() + (d.getTimezoneOffset() * 60000);
      const nd = new Date(utc + (3600000 * 8));
    
      if (nextDay === undefined) {
        nextDay = 1;
      }
      if (nd.getDay() === 5 && nd.getHours() > 15) {
        nextDay = 3;
      } else if ([0, 6].includes(nd.getDay()) || nd.getHours() > 15) {
        nextDay = 2;
      }
      const day = new Date();
      const tomorrow = new Date(day);
      tomorrow.setDate(tomorrow.getDate() + nextDay);
      const yy = tomorrow.getFullYear();
      let mm = tomorrow.getMonth() + 1;
      if (mm < 10) {
        mm = `0${mm}`;
      }
      let dd = tomorrow.getDate();
      if (dd < 10) {
        dd = `0${dd}`;
      }
      const available = `${yy}-${mm}-${dd}`;
      if (publicHolidays.includes(available)) {
        const nextDay = 7;
        for (let i = 2; i < nextDay; i += 1) {
          availableDay(i);
        }
      } else {
        console.log('returning available', available);
        return(available);
      }
    }
    
    availableDay()
    2 回复  |  直到 6 年前
        1
  •  1
  •   Jaromanda X    6 年前

    我认为这个逻辑会起作用-我已经创建了一个函数来执行“日期字符串”- yyyy-mm-dd “因为它现在在两个地方都有使用

    我也会在周末 tomorrow.getDay() % 6 === 0 [0, 6].includes(tomorrow.getDay()) 如果你愿意的话

    const publicHolidays = ['2018-09-28', '2018-12-25']
    
    const availableDay = () => {
        let nextDay = 1; // since we are not recursive any more
        const d = new Date();
        const utc = d.getTime() + (d.getTimezoneOffset() * 60000);
        const nd = new Date(utc + (3600000 * 8));
    
        if (nd.getDay() === 5 && nd.getHours() > 15) {
            nextDay = 3;
        } else if ([0, 6].includes(nd.getDay()) || nd.getHours() > 15) {
            nextDay = 2;
        }
        const day = new Date();
        const tomorrow = new Date(day);
        tomorrow.setDate(tomorrow.getDate() + nextDay);
        // changes start here
        const dateString = d => `${.getFullYear()}-${('0' + (d.getMonth() + 1)).toString(-2)}-${('0' + d.getDate()).toString(-2)}`;
    
        let available = dateString(tomorrow);
        while (publicHolidays.includes(available) || (tomorrow.getDay() === 0)) {
            tomorrow.setDate(tomorrow.getDate() + 1);
            available = dateString(tomorrow);
        }
        console.log('returning available', available);
        return(available);
    }
    
    availableDay()
    

    您可以做更多的工作来简化代码,但这至少可以解决问题

        2
  •  0
  •   shadow    6 年前

    我想你应该一直+1到下一天。所以如果今天是公共假日,试着第二天去。循环重复,直到不是公共假日。

    if (publicHolidays.includes(available)) {
        availableDay(nextDay +1 );
    } else {
        console.log('returning available', available);
        return(available);
    }