Zoho Deluge - First Monday of Month or Last Tuesday of Month

What?
An article on setting a date field to either the first Monday of the next month or to the last Tuesday of the current/next month. What I mean by the last Tuesday is if the last Tuesday of the month is before the current date, then set it to the last Tuesday of next month.

Why?
This was a request by a client who instead of specifying the 1st of every month, or 15th of each month, to say the first Monday of the month (ignoring bank holidays) or to say the last Tuesday of the month.

How?
Quite easily. For the first Monday of the month, it is likely that even if today was the first day of the month and coincidentally a Monday, then we would want to specify the field date to be the first Monday of the next month. If however we want the last Tuesday of the month and that Tuesday happens before today, then we want the last Tuesday of the next month.

In the following code, consider that the date field we want to set is called "Switch_Over_Day". For the first Monday, we are going to select the first day of the month and then loop until we find a Monday. Same for the last Tuesday except that we will count backwards from the end of the month:
if(input.Switch_Over_Day=="First Monday of Month")
{
    v_NextMonthDate = zoho.currentdate.addMonth(1);
    v_NextMonthDateFirst = v_NextMonthDate.toStartOfMonth();
    if(v_NextMonthDateFirst.getDayOfWeek()!=2)
    {
        for each v_MyDay in {1,2,3,4,5,6,7,8}
        {
            v_NextMonthDateFirst = v_NextMonthDateFirst.toDate();
            v_NextMonthMonday = v_NextMonthDateFirst.addDay(v_MyDay);
            if(v_NextMonthMonday.getDayOfWeek()==2)
            {
                break;
            }
        } 
        input.Next_Switch_Date = v_NextMonthMonday.toDate();
    }
}
else if(input.Switch_Over_Day=="Last Tuesday of Month")
{
    v_NextMonthDate = zoho.currentdate.addMonth(1);
    v_NextMonthDateLast = v_NextMonthDate.toStartOfMonth().subDay(1);
    if(v_NextMonthDateLast.getDayOfWeek()!=3)
    {
        for each v_MyDay in {1,2,3,4,5,6,7,8}
        {
            v_NextMonthDateLast = v_NextMonthDateLast.toDate();
            v_NextMonthTuesday = v_NextMonthDateLast.subDay(v_MyDay);
            if(v_NextMonthTuesday.getDayOfWeek()==3)
            {
                break;
            }
        } 
        // if last Tuesday is before today
        if(v_NextMonthTuesday.toDate() <= zoho.currentdate)
        {
            v_NextMonthDate = zoho.currentdate.addMonth(2);
            v_NextMonthDateLast = v_NextMonthDate.toStartOfMonth().subDay(1);
            if(v_NextMonthDateLast.getDayOfWeek()!=3)
            {
                for each v_MyDay in {1,2,3,4,5,6,7,8}
                {
                    v_NextMonthDateLast = v_NextMonthDateLast.toDate();
                    v_NextMonthTuesday = v_NextMonthDateLast.subDay(v_MyDay);
                    if(v_NextMonthTuesday.getDayOfWeek()==3)
                    {
                        break;
                    }
                } 
            }
        }
        input.Next_Switch_Date = v_NextMonthTuesday.toDate();
    }
}

All Variations
How about the code for all variations such as "First Wednesday of Month", "Third Thursday of Month", "Last Friday of Month"? So in the following code we do this, we're checking that the option selected doesn't have a digit and then checking for a day name and then checking if "First", "Second", "Third", "Fourth" or "Last" day (no point in "Fifth" as "Last" ought to be similar):
v_DayFreq = 0;
l_AllDays = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32};
v_InputSwitch = input.Switch_Over_Day;
v_InputSwitchCheck = v_InputSwitch.replaceAll("[^0-9]","");
if(v_InputSwitchCheck == "")
{
	// what day of week
	if(v_InputSwitch.toString().indexOf("Monday") > 0)
	{
		v_DayIndex = 2;
	}
	else if(v_InputSwitch.toString().indexOf("Tuesday") > 0)
	{
		v_DayIndex = 3;
	}
	else if(v_InputSwitch.toString().indexOf("Wednesday") > 0)
	{
		v_DayIndex = 4;
	}
	else if(v_InputSwitch.toString().indexOf("Thursday") > 0)
	{
		v_DayIndex = 5;
	}
	else if(v_InputSwitch.toString().indexOf("Friday") > 0)
	{
		v_DayIndex = 6;
	}
	// occurrence
	if(v_InputSwitch.toString().indexOf("First") >= 0)
	{
		v_DayOccur = 1;
	}
	else if(v_InputSwitch.toString().indexOf("Second") >= 0)
	{
		v_DayOccur = 2;
	}
	else if(v_InputSwitch.toString().indexOf("Third") >= 0)
	{
		v_DayOccur = 3;
	}
	else if(v_InputSwitch.toString().indexOf("Fourth") >= 0)
	{
		v_DayOccur = 4;
	}
	else if(v_InputSwitch.toString().indexOf("Last") >= 0)
	{
		v_DayOccur = -1;
	}
	//
	// calculate date
	v_NextMonthDate = zoho.currentdate;
	if(v_DayOccur > 0)
	{
		v_NextDateStart = v_NextMonthDate.toStartOfMonth().subDay(1);
		for each  v_MyDay in l_AllDays
		{
			v_NextDate = v_NextDateStart.addDay(v_MyDay);
			if(v_NextDate.getDayOfWeek() == v_DayIndex)
			{
				v_DayFreq = v_DayFreq + 1;
				if(v_DayOccur == v_DayFreq)
				{
					break;
				}
			}
		}
		// if next Day is today or before today
		if(v_NextDate.toDate() <= zoho.currentdate)
		{
			v_DayFreq = 0;
			v_NextMonthDate = zoho.currentdate.addMonth(1);
			v_NextDateStart = v_NextMonthDate.toStartOfMonth();
			if(v_NextDateStart.getDayOfWeek() != v_DayIndex)
			{
				for each  v_MyDay in l_AllDays
				{
					v_NextDate = v_NextDateStart.addDay(v_MyDay);
					if(v_NextDate.getDayOfWeek() == v_DayIndex)
					{
						v_DayFreq = v_DayFreq + 1;
						if(v_DayOccur == v_DayFreq)
						{
							break;
						}
					}
				}
			}
			else
			{
				v_NextDate = v_NextDateStart.toDate();
			}
		}
	}
	else
	{
		v_NextMonthDate = zoho.currentdate.addMonth(1);
		v_NextDateStart = v_NextMonthDate.toStartOfMonth().subDay(1);
		if(v_NextDateStart.getDayOfWeek() != v_DayIndex)
		{
			for each  v_MyDay in l_AllDays
			{
				v_NextDate = v_NextDateStart.subDay(v_MyDay);
				if(v_NextDate.getDayOfWeek() == v_DayIndex)
				{
					break;
				}
			}
			// if last Day is before today
			if(v_NextDate.toDate() <= zoho.currentdate)
			{
				v_NextMonthDate = zoho.currentdate.addMonth(2);
				v_NextDateStart = v_NextMonthDate.toStartOfMonth().subDay(1);
				if(v_NextDateStart.getDayOfWeek() != v_DayIndex)
				{
					for each  v_MyDay in l_AllDays
					{
						v_NextDate = v_NextMonthDate.subDay(v_MyDay);
						if(v_NextDate.getDayOfWeek() == v_DayIndex)
						{
							break;
						}
					}
				}
			}
		}
		else
		{
			v_NextDate = v_NextDateStart.toDate();
		}
	}
	input.Next_Switch_Date = v_NextDate.toDate();
}

Additional Note(s):
  • The first day of the week in Creator is Sunday=1
  • The day index is as follows: Sunday=1, Monday=2, Tuesday=3, Wednesday=4, Thursday=5, Friday=6, Saturday=7

Related Articles

Joes Revolver Map

Accreditation

Badge - Certified Zoho Creator Associate
Badge - Certified Zoho Creator Associate

Donate & Support

If you like my content, and would like to support this sharing site, feel free to donate using a method below:

Paypal:
Donate to Joel Lipman via PayPal

Bitcoin:
Donate to Joel Lipman with Bitcoin - Valid till 8 May 2022 3QnhmaBX7LQSRsC9hh6Je9rGQKEGNQNfPb
© 2021 Joel Lipman .com. All Rights Reserved.