[PR #24641] fix(schedule): correct weekly frequency weekday calculation algorithm #30726

Closed
opened 2026-02-21 20:48:06 -05:00 by yindo · 0 comments
Owner

Original Pull Request: https://github.com/langgenius/dify/pull/24641

State: closed
Merged: Yes


Summary

Fixed a mathematical error in the weekly schedule trigger's weekday calculation algorithm that caused incorrect weekday display in both node and panel views.

Problem

The original implementation incorrectly used weekday index values (0-6) directly as day offsets when calculating execution dates. This caused:

  • Selecting Sunday would display Wednesday (using 0 as offset)
  • Selecting Monday would display Thursday (using 1 as offset)
  • Weekday selection in panel did not match the displayed execution time

Root Cause

The bug was in execution-time-calculator.ts at line 124:

// Incorrect: using weekday index as day offset
execution.setDate(userToday.getDate() + targetDay + (weekOffset * 7))

Solution

Replaced the incorrect calculation with proper weekday arithmetic:

// Correct: calculate actual days until target weekday
const currentDayOfWeek = userToday.getDay()
const daysUntilTarget = (targetDay - currentDayOfWeek + 7) % 7
const adjustedDays = daysUntilTarget === 0 ? 7 : daysUntilTarget
execution.setDate(userToday.getDate() + adjustedDays + (weekOffset * 7))

Additional Improvements

  • Added validation to skip invalid weekday values
  • Added protection against infinite loops when no valid weekdays exist
  • Enhanced robustness of the weekly calculation logic

Impact

  • Only affects weekly frequency mode in schedule triggers
  • No impact on hourly, daily, or monthly frequency modes
  • No changes to UI components or time zone handling
  • Fixes weekday display inconsistency between selector and execution time

Testing

The fix has been verified with various scenarios:

  • Wednesday → Sunday selection: correctly calculates 4 days ahead
  • Wednesday → Monday selection: correctly calculates 5 days ahead
  • Same weekday selection: correctly jumps to next week
  • Cross-week boundary calculations work properly

Checklist

  • This change requires a documentation update, included: Dify Document
  • I understand that this PR may be closed in case there was no previous discussion or issues. (This doesn't apply to typos!)
  • I've added a test for each change that was introduced, and I tried as much as possible to make a single atomic change.
  • I've updated the documentation accordingly.
  • I ran dev/reformat(backend) and cd web && npx lint-staged(frontend) to appease the lint gods
**Original Pull Request:** https://github.com/langgenius/dify/pull/24641 **State:** closed **Merged:** Yes --- ## Summary Fixed a mathematical error in the weekly schedule trigger's weekday calculation algorithm that caused incorrect weekday display in both node and panel views. ### Problem The original implementation incorrectly used weekday index values (0-6) directly as day offsets when calculating execution dates. This caused: - Selecting Sunday would display Wednesday (using 0 as offset) - Selecting Monday would display Thursday (using 1 as offset) - Weekday selection in panel did not match the displayed execution time ### Root Cause The bug was in `execution-time-calculator.ts` at line 124: ```typescript // Incorrect: using weekday index as day offset execution.setDate(userToday.getDate() + targetDay + (weekOffset * 7)) ``` ### Solution Replaced the incorrect calculation with proper weekday arithmetic: ```typescript // Correct: calculate actual days until target weekday const currentDayOfWeek = userToday.getDay() const daysUntilTarget = (targetDay - currentDayOfWeek + 7) % 7 const adjustedDays = daysUntilTarget === 0 ? 7 : daysUntilTarget execution.setDate(userToday.getDate() + adjustedDays + (weekOffset * 7)) ``` ### Additional Improvements - Added validation to skip invalid weekday values - Added protection against infinite loops when no valid weekdays exist - Enhanced robustness of the weekly calculation logic ### Impact - Only affects weekly frequency mode in schedule triggers - No impact on hourly, daily, or monthly frequency modes - No changes to UI components or time zone handling - Fixes weekday display inconsistency between selector and execution time ### Testing The fix has been verified with various scenarios: - Wednesday → Sunday selection: correctly calculates 4 days ahead - Wednesday → Monday selection: correctly calculates 5 days ahead - Same weekday selection: correctly jumps to next week - Cross-week boundary calculations work properly ## Checklist - [ ] This change requires a documentation update, included: [Dify Document](https://github.com/langgenius/dify-docs) - [x] I understand that this PR may be closed in case there was no previous discussion or issues. (This doesn't apply to typos!) - [x] I've added a test for each change that was introduced, and I tried as much as possible to make a single atomic change. - [x] I've updated the documentation accordingly. - [x] I ran `dev/reformat`(backend) and `cd web && npx lint-staged`(frontend) to appease the lint gods
yindo added the pull-request label 2026-02-21 20:48:06 -05:00
yindo closed this issue 2026-02-21 20:48:06 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langgenius/dify#30726