For Zoho Services only:


I'm actually part of a bigger team at Ascent Business Solutions where we have support technicians and project consultants. Support is for smaller technical fixes but this can include developments, reports or integrations; depending on the size of the task. Projects are for more time-consuming developments such as revamps of the Zoho Suite of apps or on-site training. The advantage of a team is that if I am out-of-office for a day or so, there is always someone at Ascent Business Solutions who can deal with any queries/issues you may have.

Our support rates can be found and purchased at http://ascentbusiness.co.uk/zoho-support-2. A support bundle doesn't have an expiry date. So whether we can do what you want within the bundle and a year later need further support, if there are minutes left on the bundle then there is no additional charge.

Our project rates for bigger developments can be found at http://ascentbusiness.co.uk/crm-solutions/zoho-crm-packages-prices and will involve a dedicated project consultant along with developers who will hold your hand through the development process.

If you want help building a solution for one of the Zoho Apps in the Zoho Suite, contact us on 0121 293 8140 (UK) or by email at info@ascentbusiness.co.uk. You can also visit our website at http://ascentbusiness.co.uk.

I regularly build and specialize in 2-way API integrations for Xero, Shopify and eBay.

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:
copyraw
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();
    }
}
  1.  if(input.Switch_Over_Day=="First Monday of Month") 
  2.  { 
  3.      v_NextMonthDate = zoho.currentdate.addMonth(1)
  4.      v_NextMonthDateFirst = v_NextMonthDate.toStartOfMonth()
  5.      if(v_NextMonthDateFirst.getDayOfWeek()!=2) 
  6.      { 
  7.          for each v_MyDay in {1,2,3,4,5,6,7,8} 
  8.          { 
  9.              v_NextMonthDateFirst = v_NextMonthDateFirst.toDate()
  10.              v_NextMonthMonday = v_NextMonthDateFirst.addDay(v_MyDay)
  11.              if(v_NextMonthMonday.getDayOfWeek()==2) 
  12.              { 
  13.                  break
  14.              } 
  15.          } 
  16.          input.Next_Switch_Date = v_NextMonthMonday.toDate()
  17.      } 
  18.  } 
  19.  else if(input.Switch_Over_Day=="Last Tuesday of Month") 
  20.  { 
  21.      v_NextMonthDate = zoho.currentdate.addMonth(1)
  22.      v_NextMonthDateLast = v_NextMonthDate.toStartOfMonth().subDay(1)
  23.      if(v_NextMonthDateLast.getDayOfWeek()!=3) 
  24.      { 
  25.          for each v_MyDay in {1,2,3,4,5,6,7,8} 
  26.          { 
  27.              v_NextMonthDateLast = v_NextMonthDateLast.toDate()
  28.              v_NextMonthTuesday = v_NextMonthDateLast.subDay(v_MyDay)
  29.              if(v_NextMonthTuesday.getDayOfWeek()==3) 
  30.              { 
  31.                  break
  32.              } 
  33.          } 
  34.          // if last Tuesday is before today 
  35.          if(v_NextMonthTuesday.toDate() <= zoho.currentdate) 
  36.          { 
  37.              v_NextMonthDate = zoho.currentdate.addMonth(2)
  38.              v_NextMonthDateLast = v_NextMonthDate.toStartOfMonth().subDay(1)
  39.              if(v_NextMonthDateLast.getDayOfWeek()!=3) 
  40.              { 
  41.                  for each v_MyDay in {1,2,3,4,5,6,7,8} 
  42.                  { 
  43.                      v_NextMonthDateLast = v_NextMonthDateLast.toDate()
  44.                      v_NextMonthTuesday = v_NextMonthDateLast.subDay(v_MyDay)
  45.                      if(v_NextMonthTuesday.getDayOfWeek()==3) 
  46.                      { 
  47.                          break
  48.                      } 
  49.                  } 
  50.              } 
  51.          } 
  52.          input.Next_Switch_Date = v_NextMonthTuesday.toDate()
  53.      } 
  54.  } 

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):
copyraw
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();
}
  1.  v_DayFreq = 0
  2.  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}
  3.  v_InputSwitch = input.Switch_Over_Day; 
  4.  v_InputSwitchCheck = v_InputSwitch.replaceAll("[^0-9]","")
  5.  if(v_InputSwitchCheck == "") 
  6.  { 
  7.      // what day of week 
  8.      if(v_InputSwitch.toString().indexOf("Monday") > 0) 
  9.      { 
  10.          v_DayIndex = 2
  11.      } 
  12.      else if(v_InputSwitch.toString().indexOf("Tuesday") > 0) 
  13.      { 
  14.          v_DayIndex = 3
  15.      } 
  16.      else if(v_InputSwitch.toString().indexOf("Wednesday") > 0) 
  17.      { 
  18.          v_DayIndex = 4
  19.      } 
  20.      else if(v_InputSwitch.toString().indexOf("Thursday") > 0) 
  21.      { 
  22.          v_DayIndex = 5
  23.      } 
  24.      else if(v_InputSwitch.toString().indexOf("Friday") > 0) 
  25.      { 
  26.          v_DayIndex = 6
  27.      } 
  28.      // occurrence 
  29.      if(v_InputSwitch.toString().indexOf("First") >= 0) 
  30.      { 
  31.          v_DayOccur = 1
  32.      } 
  33.      else if(v_InputSwitch.toString().indexOf("Second") >= 0) 
  34.      { 
  35.          v_DayOccur = 2
  36.      } 
  37.      else if(v_InputSwitch.toString().indexOf("Third") >= 0) 
  38.      { 
  39.          v_DayOccur = 3
  40.      } 
  41.      else if(v_InputSwitch.toString().indexOf("Fourth") >= 0) 
  42.      { 
  43.          v_DayOccur = 4
  44.      } 
  45.      else if(v_InputSwitch.toString().indexOf("Last") >= 0) 
  46.      { 
  47.          v_DayOccur = -1
  48.      } 
  49.      // 
  50.      // calculate date 
  51.      v_NextMonthDate = zoho.currentdate
  52.      if(v_DayOccur > 0) 
  53.      { 
  54.          v_NextDateStart = v_NextMonthDate.toStartOfMonth().subDay(1)
  55.          for each  v_MyDay in l_AllDays 
  56.          { 
  57.              v_NextDate = v_NextDateStart.addDay(v_MyDay)
  58.              if(v_NextDate.getDayOfWeek() == v_DayIndex) 
  59.              { 
  60.                  v_DayFreq = v_DayFreq + 1
  61.                  if(v_DayOccur == v_DayFreq) 
  62.                  { 
  63.                      break
  64.                  } 
  65.              } 
  66.          } 
  67.          // if next Day is today or before today 
  68.          if(v_NextDate.toDate() <= zoho.currentdate) 
  69.          { 
  70.              v_DayFreq = 0
  71.              v_NextMonthDate = zoho.currentdate.addMonth(1)
  72.              v_NextDateStart = v_NextMonthDate.toStartOfMonth()
  73.              if(v_NextDateStart.getDayOfWeek() != v_DayIndex) 
  74.              { 
  75.                  for each  v_MyDay in l_AllDays 
  76.                  { 
  77.                      v_NextDate = v_NextDateStart.addDay(v_MyDay)
  78.                      if(v_NextDate.getDayOfWeek() == v_DayIndex) 
  79.                      { 
  80.                          v_DayFreq = v_DayFreq + 1
  81.                          if(v_DayOccur == v_DayFreq) 
  82.                          { 
  83.                              break
  84.                          } 
  85.                      } 
  86.                  } 
  87.              } 
  88.              else 
  89.              { 
  90.                  v_NextDate = v_NextDateStart.toDate()
  91.              } 
  92.          } 
  93.      } 
  94.      else 
  95.      { 
  96.          v_NextMonthDate = zoho.currentdate.addMonth(1)
  97.          v_NextDateStart = v_NextMonthDate.toStartOfMonth().subDay(1)
  98.          if(v_NextDateStart.getDayOfWeek() != v_DayIndex) 
  99.          { 
  100.              for each  v_MyDay in l_AllDays 
  101.              { 
  102.                  v_NextDate = v_NextDateStart.subDay(v_MyDay)
  103.                  if(v_NextDate.getDayOfWeek() == v_DayIndex) 
  104.                  { 
  105.                      break
  106.                  } 
  107.              } 
  108.              // if last Day is before today 
  109.              if(v_NextDate.toDate() <= zoho.currentdate) 
  110.              { 
  111.                  v_NextMonthDate = zoho.currentdate.addMonth(2)
  112.                  v_NextDateStart = v_NextMonthDate.toStartOfMonth().subDay(1)
  113.                  if(v_NextDateStart.getDayOfWeek() != v_DayIndex) 
  114.                  { 
  115.                      for each  v_MyDay in l_AllDays 
  116.                      { 
  117.                          v_NextDate = v_NextMonthDate.subDay(v_MyDay)
  118.                          if(v_NextDate.getDayOfWeek() == v_DayIndex) 
  119.                          { 
  120.                              break
  121.                          } 
  122.                      } 
  123.                  } 
  124.              } 
  125.          } 
  126.          else 
  127.          { 
  128.              v_NextDate = v_NextDateStart.toDate()
  129.          } 
  130.      } 
  131.      input.Next_Switch_Date = v_NextDate.toDate()
  132.  } 

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
Category: Zoho :: Article: 695

Credit where Credit is Due:


Feel free to copy, redistribute and share this information. All that we ask is that you attribute credit and possibly even a link back to this website as it really helps in our search engine rankings.

Disclaimer: The information on this website is provided without warranty and any content is merely the opinion of the author. Please try to test in development environments prior to adapting them to your production environments. The articles are written in good faith and, at the time of print, are working examples used in a commercial setting.

Thank you for visiting and, as always, we hope this website was of some use to you!

Kind Regards,

Joel Lipman
www.joellipman.com

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.