To anchor a date to a specific day of the week - to either roll forward, or backward, to a specific day in the week.
nextWeekDay()
- Zoho Documentation.previousWeekDay()
- Zoho Documentation.addWeek()
- Zoho Documentation.addMonth()
- Zoho Documentation.addYear()
- Zoho Documentation.
Given a time and if it falls under the USA daylight savings, to either offset the time by 1 hour, or not.
USA daylight savings is applicable between:
- The second Sunday of March, to
- The first Sunday of November.
Given the Business Use Case, using the nextWeekday()
function will be sufficient as we will be performing the following steps:
graph TD
A[Get today] --> B[Set to <b>First day</b> of March]
B -->|startDate| C[Set to <b>following Sunday</b> in March<br>to get <b>First Sunday</b> of March]
C --> D[Add <b>1 week</b> to startDate<br>to get <b>Second Sunday</b> of March]
D -->|daylightSavingsStart| E(<b>END</b>)
B --> F[Add 8 months]
F --> G[Get <b>First day</b> of November]
G -->|endDate|H[Set to following Sunday in November<br>to get <b>First Sunday</b> of November]
H -->|daylightSavingsEnd| E
//To produce a date range starting from the 2nd Sunday in March to the 1st Sunday of November in the year.
startDate = (today.getYear().toString() + "-03-01").toDate();
endDate = startDate.addMonth(8);
daylightSavingsStart = startDate.nextWeekDay("Sunday").addWeek(1);
daylightSavingsEnd = endDate.nextWeekDay("Sunday");
When you run your info
statements on daylightSavingsStart
and daylightSavingsEnd
, and compare them against startDate
and endDate
, you should see the the expected results.
info "Start of March: " + startDate + " (" + startDate.toString("E") + ")" + " | " + "Daylight Savings Start: " + daylightSavingsStart + " (" + daylightSavingsStart.toString("E") + ")";
info "Start of November: " + endDate + " (" + endDate.toString("E") + ")" + " | " + "Daylight Savings End: " + daylightSavingsEnd + " (" + daylightSavingsEnd.toString("E") + ")";