Calculate difference between two dates Excluding Weekend and Holiday date in SharePoint 2013 or SharePoint 2010

SharePoint 2013 calculate difference between two dates

Here we discuss how we find the number of total working days between two dates Excluding Weekend and Holiday’s date in SharePoint 2013/2010. For this, we need to create two lists. The first list is for whole details where we store the Start_date, End_date and Number_of_days. The second list for holiday’s detail’s where we store Holiday’s_name and date. Now I want to calculate the total number of working days when we add the item in the first list. So for this, I have created the Event handler under the first list using visual studio and write the logic under the ItemAdded method in SharePoint.

Also, you can read some SharePoint tutorials:
SharePoint online: Ultimate tutorial guide PDF download

SharePoint Online modern experience

How to move from classic to modern experience in SharePoint online list or document library?

For this follow these steps:

1. This is the code of AddItem Event handler that’s created under first list. See in fig.

using System;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Workflow;

namespace SharePointLeaveApplication.SPLeave_Details.ListInstance3.AddItemEvent
{

public class AddItemEvent : SPItemEventReceiver
{
public override void ItemAdded(SPItemEventProperties properties)
{
base.ItemAdded(properties);
SPListItem item = properties.ListItem;

using(SPWeb web=properties.OpenWeb())
{

SPList list =web.Lists[properties.ListId];

<!– Here we fetches the value of start_date and End_date from first list –>

DateTime startdate =Convert.ToDateTime(properties.ListItem[“Start_Date”].ToString());
DateTime enddate = Convert.ToDateTime(properties.ListItem[“End_Date”].ToString());

int number_of_days = 0;
int workingday = getWorkingDays(startdate, enddate);

<!– Here we fetches the second list Holidays –>
SPList listinstance = web.Lists[“Official Holidays”];

number_of_days = workingday – getHolidays(listinstance, startdate, enddate);
item[“Number_of_Leave”] = number_of_days;

item.Update();
list.Update();
web.AllowUnsafeUpdates = false;
}
}

<!– Here we call the method for getWorkingDays –>

public static int getWorkingDays(DateTime startdate, DateTime enddate)
{

int totaldays = 0;
for (var date = startdate; date <=enddate; date = date.AddDays(1))
{
if (date.DayOfWeek != DayOfWeek.Sunday)
totaldays++;
}
return totaldays;
}

<!– Here we call the method for getHolidays from Second list –>

public static int getHolidays(SPList list,DateTime startDate,DateTime endDate)
{

SPListItemCollection holidayitemcollection = list.Items;

if (holidayitemcollection.Count > 0)
{
DateTime datetocheck = DateTime.Now;
int noOfHoliDays = 0;
foreach (SPListItem holidayItem in holidayitemcollection)
{
datetocheck = Convert.ToDateTime(holidayItem[“Date”]);
if ((datetocheck >= startDate) && (datetocheck <= endDate))
{
noOfHoliDays++;
}
}
return noOfHoliDays;
}
else
{
return 0;
}

}
}

SharePoint 2013 calculate difference between two dates
SharePoint 2013 calculate difference between two dates

2. If you go proper way than you will find the the result as see in fig. In first fig I have create two holiday and In second fig see the Selected date is coming two Sunday and Two holidays . So see the result number of working day’s excluding Sunday and Holidays.

Second List (Holiday details)

SharePoint 2013 calculate difference between two dates excluding weekends
SharePoint 2013 calculate the difference between two dates excluding weekends

First List (all details)

calculate difference between two dates excluding weekends in SharePoint 2013
calculate the difference between two dates excluding weekends in SharePoint 2013

Hope this article will be helpful.

Check out Best Alternative to InfoPath -> Try Now

You May Also like the Following SharePoint Online Tutorials:

About Navneet Singh

I am Navneet Singh, Working at C3it software solutions pvt. ltd. in Pune. My core skills are SharePoint 2013, Office 365, SharePoint 2010, SharePoint Designer, SharePoint Designer Workflow, Nintex Workflow, Infopath, Webpart, CSOM/SSOM, Power-shell, C#, ASP.NET and SQL Server.

View all posts by Navneet Singh →