A very quick article with a snippet of code to get today's date, tomorrow's, and the day after's but it has to skip Sundays.
Why?
Wanting to create a schedule for availability over the next few days which needs to dynamically change. I am aware of the function .addBusinessDay() but that excludes Saturdays which my client still works on.
How?
We're going to get today's date, then use the .addDay(), check none of these are a Sunday, and move the next few days along:
thisDate = '2021-10-30'; v_Today = thisDate; v_Tomorrow = v_Today.addDay(1); v_NextDay = v_Today.addDay(2); if(v_Today.getDayOfWeek()==1) { v_Today = v_Today.addDay(1); v_Tomorrow = v_Tomorrow.addDay(1); v_NextDay = v_NextDay.addDay(1); } // if(v_Tomorrow.getDayOfWeek()==1) { v_Tomorrow = v_Tomorrow.addDay(1); v_NextDay = v_NextDay.addDay(1); } // if(v_NextDay.getDayOfWeek()==1) { v_NextDay = v_NextDay.addDay(1); } info v_Today.toString("EEEE dd-MMM-yy"); info v_Tomorrow.toString("EEEE dd-MMM-yy"); info v_NextDay.toString("EEEE dd-MMM-yy"); // // yields: // Saturday 30-Oct-21 // Monday 01-Nov-21 // Tuesday 02-Nov-21
- thisDate = '2021-10-30';
- v_Today = thisDate;
- v_Tomorrow = v_Today.addDay(1);
- v_NextDay = v_Today.addDay(2);
- if(v_Today.getDayOfWeek()==1)
- {
- v_Today = v_Today.addDay(1);
- v_Tomorrow = v_Tomorrow.addDay(1);
- v_NextDay = v_NextDay.addDay(1);
- }
- //
- if(v_Tomorrow.getDayOfWeek()==1)
- {
- v_Tomorrow = v_Tomorrow.addDay(1);
- v_NextDay = v_NextDay.addDay(1);
- }
- //
- if(v_NextDay.getDayOfWeek()==1)
- {
- v_NextDay = v_NextDay.addDay(1);
- }
- info v_Today.toString("EEEE dd-MMM-yy");
- info v_Tomorrow.toString("EEEE dd-MMM-yy");
- info v_NextDay.toString("EEEE dd-MMM-yy");
- //
- // yields:
- // Saturday 30-Oct-21
- // Monday 01-Nov-21
- // Tuesday 02-Nov-21
Note that Sunday is day 1 of the week. Saturday is day 7 of the week.
Here's the English ordinal bit I used:
m_Ordinals = {1:"st",21:"st",31:"st",2:"nd",22:"nd",3:"rd",23:"rd"}; v_Day3_Ordinal = ifnull(m_Ordinals.get(v_NextDay.toString("d").toLong()),"th");
- m_Ordinals = {1:"st",21:"st",31:"st",2:"nd",22:"nd",3:"rd",23:"rd"};
- v_Day3_Ordinal = ifnull(m_Ordinals.get(v_NextDay.toString("d").toLong()),"th");
v_Day1_Header = "<p>Today<br /><b>" + v_Today.toString("EEEE") + "</b><br />" + v_Today.toString("MMMM") + "</p>"; v_Day2_Header = "<p>Tomorrow<br /><b>" + v_Tomorrow.toString("EEEE") + "</b><br />" + v_Tomorrow.toString("MMMM") + "</p>"; v_Day3_Header = "<p>" + v_NextDay.toString("d") + v_Day3_Ordinal + "<br /><b>" + v_NextDay.toString("EEEE") + "</b><br />" + v_NextDay.toString("MMMM") + "</p>";
- v_Day1_Header = "<p>Today<br /><b>" + v_Today.toString("EEEE") + "</b><br />" + v_Today.toString("MMMM") + "</p>";
- v_Day2_Header = "<p>Tomorrow<br /><b>" + v_Tomorrow.toString("EEEE") + "</b><br />" + v_Tomorrow.toString("MMMM") + "</p>";
- v_Day3_Header = "<p>" + v_NextDay.toString("d") + v_Day3_Ordinal + "<br /><b>" + v_NextDay.toString("EEEE") + "</b><br />" + v_NextDay.toString("MMMM") + "</p>";
Generate a list of working dates given a number of days
So here's another scenario where you have a start date and you are asked to list the working days, 7 days after the start date. I'm aware of the method .workdaysList():
date1='2022-07-21'; date2='2022-07-29'; info date1.workdayslist(date2); // yields: Thu Jul 21 00:00:00 PDT 2022,Fri Jul 22 00:00:00 PDT 2022,Mon Jul 25 00:00:00 PDT 2022,Tue Jul 26 00:00:00 PDT 2022,Wed Jul 27 00:00:00 PDT 2022,Thu Jul 28 00:00:00 PDT 2022,Fri Jul 29 00:00:00 PDT 2022
- date1='2022-07-21';
- date2='2022-07-29';
- info date1.workdayslist(date2);
- // yields: Thu Jul 21 00:00:00 PDT 2022,Fri Jul 22 00:00:00 PDT 2022,Mon Jul 25 00:00:00 PDT 2022,Tue Jul 26 00:00:00 PDT 2022,Wed Jul 27 00:00:00 PDT 2022,Thu Jul 28 00:00:00 PDT 2022,Fri Jul 29 00:00:00 PDT 2022
// // initialize list to hold valid appointment dates l_AppointmentDates = List(); // // initialize Start Date v_GivenDate = '2022-07-21'; // // setup list to loop through (= number of days) l_NumberOfDays = {1,2,3,4,5,6,7}; // // start with first date to add (check if it is a working day) v_NextWorkingDay = v_GivenDate; // // loop through each day to set an appointment for for each index v_Loop in l_NumberOfDays { // skip if Sunday (add 1 day) if(v_NextWorkingDay.getDayOfWeek()==1) { v_NextWorkingDay = v_NextWorkingDay.addDay(1); } // skip if Saturday (add 2 days) else if(v_NextWorkingDay.getDayOfWeek()==7) { v_NextWorkingDay = v_NextWorkingDay.addDay(2); } // add to list of allowed dates l_AppointmentDates.add(v_NextWorkingDay); // // output and add 1 more day to loop info v_NextWorkingDay.toString("EEEE") + " :: " + v_NextWorkingDay.toString("yyyy-MM-dd"); v_NextWorkingDay = v_NextWorkingDay.addDay(1); }
- //
- // initialize list to hold valid appointment dates
- l_AppointmentDates = List();
- //
- // initialize Start Date
- v_GivenDate = '2022-07-21';
- //
- // setup list to loop through (= number of days)
- l_NumberOfDays = {1,2,3,4,5,6,7};
- //
- // start with first date to add (check if it is a working day)
- v_NextWorkingDay = v_GivenDate;
- //
- // loop through each day to set an appointment for
- for each index v_Loop in l_NumberOfDays
- {
- // skip if Sunday (add 1 day)
- if(v_NextWorkingDay.getDayOfWeek()==1)
- {
- v_NextWorkingDay = v_NextWorkingDay.addDay(1);
- }
- // skip if Saturday (add 2 days)
- else if(v_NextWorkingDay.getDayOfWeek()==7)
- {
- v_NextWorkingDay = v_NextWorkingDay.addDay(2);
- }
- // add to list of allowed dates
- l_AppointmentDates.add(v_NextWorkingDay);
- //
- // output and add 1 more day to loop
- info v_NextWorkingDay.toString("EEEE") + " :: " + v_NextWorkingDay.toString("yyyy-MM-dd");
- v_NextWorkingDay = v_NextWorkingDay.addDay(1);
- }
l_AppointmentDates = List(); v_GivenDate = '2022-07-21'; l_NumberOfDays = {1,2,3,4,5,6,7}; v_NextWorkingDay = v_GivenDate; for each index v_Loop in l_NumberOfDays { l_AppointmentDates.add(v_NextWorkingDay); info v_NextWorkingDay.toString("EEEE") + " :: " + v_NextWorkingDay.toString("yyyy-MM-dd"); v_NextWorkingDay = v_NextWorkingDay.addBusinessDay(1); }
- l_AppointmentDates = List();
- v_GivenDate = '2022-07-21';
- l_NumberOfDays = {1,2,3,4,5,6,7};
- v_NextWorkingDay = v_GivenDate;
- for each index v_Loop in l_NumberOfDays
- {
- l_AppointmentDates.add(v_NextWorkingDay);
- info v_NextWorkingDay.toString("EEEE") + " :: " + v_NextWorkingDay.toString("yyyy-MM-dd");
- v_NextWorkingDay = v_NextWorkingDay.addBusinessDay(1);
- }