【问题标题】:Get Cron Expression from Start Dates, Weekly, Monthly, and Annually从开始日期、每周、每月和每年获取 Cron 表达式
【发布时间】:2021-06-11 01:39:11
【问题描述】:

Typescript 中有没有办法从开始日期派生 Cron 表达式,用于每周、每月和每年。我一直在寻找现有的代码库。

【问题讨论】:

    标签: javascript typescript cron


    【解决方案1】:
      getCreateWeeklyFromStartDate(startDate: Date): string {
        const startWeekday = startDate.getDay();
        const startHour = startDate.getHours();
        const startMinute = startDate.getMinutes();
        const cronExpression = `${startMinute} ${startHour} * * ${startWeekday}`;
        return cronExpression; 
      }
    
      getCreateMonthlyFromStartDate(startDate: Date): string {
        const startDayOfMonth = startDate.getDate();
        const startHour = startDate.getHours();
        const startMinute = startDate.getMinutes();
        const cronExpression = `${startMinute} ${startHour} ${startDayOfMonth} * *`;
        return cronExpression;
      }
    
      getCreateAnnualFromStartDate(startDate: Date): string {
        const startMonth = startDate.getMonth() + 1;
        const startDayOfMonth = startDate.getDate();
        const startHour = startDate.getHours();
        const startMinute = startDate.getMinutes();
        const cronExpression = `${startMinute} ${startHour} ${startDayOfMonth} ${startMonth} *`;
        return cronExpression;
      }
    

    【讨论】:

      猜你喜欢
      • 2012-04-26
      • 1970-01-01
      • 2012-04-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-30
      • 2016-04-28
      相关资源
      最近更新 更多