Tip 6: How to calculate the business days between two dates.

Tip 6: How to calculate the business days between two dates.



Hi everyone,


Following our Zoho Creator - Tips and Tricks series every fortnight, we are back today with a tip based on one of the most asked questions in the forum. That's right. This tip would help you calculate the business days between two dates excluding the weekend.

For example, if wish to know the number of working days in a particular month, you need to write a custom function to do this calculation.

Functions are nothing but a unit of code written to perform a specific task.


Let's see how to do this:


  1. // Here WorkDays is the name of the function.
  2. int WorkDays(date start_date, date end_date)
  3. {
  4. //total_days will store the value of numbers of days between start date and end date.
  5. total_days=(days360(input.start_date,input.end_date) +1);
  6. counter=leftpad("1",total_days).replaceAll(" ","1,").toList();
  7. //Here counter will generate a list like {1,1,1,1,1,1} with number of 1's based on value returned in total_days.
  8. week_ends=0;
  9. date_counter=input.start_date;
  10. for each index n in counter
  11. {
  12. //Use getDayOfWeek() to identify if it is a weekday or weekend. 
  13. if((date_counter.getDayOfWeek() == 1) || (date_counter.getDayOfWeek() == 7))
  14. {
  15. week_ends=(week_ends + 1);
  16. }
  17. //Now add one day for the next loop
  18.  date_counter=date_counter.addDay(1);}
  19. total_business_days=(total_days - week_ends);
  20. return total_business_days;
  21. }

 

Let's look at some of the built-in functions that we used in the above code:

days360 is a built-in function that returns the number of days between two given dates.


getDayOfWeek is a built-in function to get the day of the week corresponding to a date. It will return a number range from 1 to 7. The number 1 represents a Sunday, 2 represents a Monday and so on.

replaceAll is a built-in function that helps you replace all the occurrence of a particular substring in a sting.


toList is a built-in function that removes the given separator in a string, and replaces it with a comma.

Isn't it easy? Give it a try. If you have any doubts or queries regarding this tip, please feel free to add them as comments below. We would be happy to address them all.




    Access your files securely from anywhere

        Zoho Developer Community




                                  Zoho Desk Resources

                                  • Desk Community Learning Series


                                  • Digest


                                  • Functions


                                  • Meetups


                                  • Kbase


                                  • Resources


                                  • Glossary


                                  • Desk Marketplace


                                  • MVP Corner


                                  • Word of the Day



                                      Zoho Marketing Automation


                                              Manage your brands on social media



                                                    Zoho TeamInbox Resources

                                                      Zoho DataPrep Resources



                                                        Zoho CRM Plus Resources

                                                          Zoho Books Resources


                                                            Zoho Subscriptions Resources

                                                              Zoho Projects Resources


                                                                Zoho Sprints Resources


                                                                  Qntrl Resources


                                                                    Zoho Creator Resources



                                                                        Zoho Campaigns Resources


                                                                          Zoho CRM Resources

                                                                          • CRM Community Learning Series

                                                                            CRM Community Learning Series


                                                                          • Kaizen

                                                                            Kaizen

                                                                          • Functions

                                                                            Functions

                                                                          • Meetups

                                                                            Meetups

                                                                          • Kbase

                                                                            Kbase

                                                                          • Resources

                                                                            Resources

                                                                          • Digest

                                                                            Digest

                                                                          • CRM Marketplace

                                                                            CRM Marketplace

                                                                          • MVP Corner

                                                                            MVP Corner





                                                                              Design. Discuss. Deliver.

                                                                              Create visually engaging stories with Zoho Show.

                                                                              Get Started Now