PH.WorkingDaysAndTime 2.0.3

.NET Standard 2.0
There is a newer version of this package available.
See the version list below for details.
dotnet add package PH.WorkingDaysAndTime --version 2.0.3
NuGet\Install-Package PH.WorkingDaysAndTime -Version 2.0.3
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="PH.WorkingDaysAndTime" Version="2.0.3" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add PH.WorkingDaysAndTime --version 2.0.3
#r "nuget: PH.WorkingDaysAndTime, 2.0.3"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install PH.WorkingDaysAndTime as a Cake Addin
#addin nuget:?package=PH.WorkingDaysAndTime&version=2.0.3

// Install PH.WorkingDaysAndTime as a Cake Tool
#tool nuget:?package=PH.WorkingDaysAndTime&version=2.0.3

A tiny c# utility for calculating work days and work time. The code is written in .NET C#.

The tool is useful for calculate difference between two dates of workdays, to plan projects excluding holidays and absences. Is also a simple starting-point to addons to estimate the date of the end of a job.

The application works only counting the dates forward and it is assumed that the date entered as the first parameter is a working day.

Features

  • can add n work-days to a DateTime;
  • can add n work-hours to a DateTime;
  • can get a List of work-DateTime between 2 dates;

Code Examples

AddWorkingDays(DateTime start, int days)

//this is the configuration of a work-week: 8h/day from monday to friday
var wts1 = new WorkTimeSpan() 
	{ Start = new TimeSpan(9, 0, 0), End = new TimeSpan(13, 0, 0) };
var wts2 = new WorkTimeSpan() 
	{ Start = new TimeSpan(14, 0, 0), End = new TimeSpan(18, 0, 0) };
var wts = new List<WorkTimeSpan>() { wts1, wts2 };

var week = new WeekDaySpan()
{
	WorkDays = new Dictionary<DayOfWeek, WorkDaySpan>()
	{
		{DayOfWeek.Monday, new WorkDaySpan() {TimeSpans = wts}}
		,
		{DayOfWeek.Tuesday, new WorkDaySpan() {TimeSpans = wts}}
		,
		{DayOfWeek.Wednesday, new WorkDaySpan() {TimeSpans = wts}}
		,
		{DayOfWeek.Thursday, new WorkDaySpan() {TimeSpans = wts}}
		,
		{DayOfWeek.Friday, new WorkDaySpan() {TimeSpans = wts}}
	}
};

//this is the configuration for holidays: 
//in Italy we have this list of Holidays plus 1 day different on each province,
//for mine is 1 Dec (see last element of the List<AHolyDay>).
var italiansHoliDays = new List<AHolyDay>()
{
	new EasterMonday(),new HoliDay(1, 1),new HoliDay(6, 1),
	new HoliDay(25, 4),new HoliDay(1, 5),new HoliDay(2, 6),
	new HoliDay(15, 8),new HoliDay(1, 11),new HoliDay(8, 12),
	new HoliDay(25, 12),new HoliDay(26, 12)
	, new HoliDay(1, 12)
};

//instantiate with configuration
var utility = new WorkingDaysAndTimeUtility(week, italiansHoliDays);

//lets-go: add 3 working-days to Jun 1, 2015
var result = utility.AddWorkingDays(new DateTime(2015,6,1), 3);
//result is Jun 5, 2015 (see holidays list) 

GetWorkingDaysBetweenTwoDateTimes(DateTime start, DateTime end, bool includeStartAndEnd = true)

var start = new DateTime(2015, 12, 31, 9, 0, 0);
var end = new DateTime(2016, 1, 7, 9, 0, 0);

//omitted configurations and holidays...
var utility = new WorkingDaysAndTimeUtility(weekConf, GetItalianHolidays());

//r is a workdays List<DateTime> between Dec 31 and Jan 7.
var r = utility.GetWorkingDaysBetweenTwoDateTimes(start, end);

Testing if given date is Working-Datetime

[Fact]
public void Get_IfWorkingDay_OnTuesday_OnSimpleWeek_ReturnTrue()
{

    //omitted configurations and holidays...
    var tuesday = new DateTime(2018, 11, 6, 11,22,33);
    var prev0 = tuesday.AddMinutes(-1);
    var next0 = tuesday.AddMinutes(1);

    var weekConf = GetSimpleWeek();
    var utility  = new WorkingDaysAndTimeUtility(weekConf, new List<HoliDay>());

    var r = utility.IfWorkingMoment(tuesday, out DateTime next, out DateTime previous);

    Assert.True(r);
    Assert.Equal(prev0, previous);
    Assert.Equal(next0, next);

}

Code Configuration Examples

Use of WorkingDaysConfig

//note thats w is WeekDaySpan and l is List<AHolyDay>
//cfg is Json serializable
var cfg = new WorkingDaysConfig(w, l);

Map-Config Style

var cfg = new WorkingDaysConfig()
                      .Week(new WeekDaySpan().Day(DayOfWeek.Monday,
                                                  new WorkDaySpan()
                                                      .Time(new TimeSpan(9, 0, 0), new TimeSpan(13, 0, 0))
                                                      .Time(new TimeSpan(14, 0, 0), new TimeSpan(18, 0, 0)))
                                             .Day(DayOfWeek.Tuesday,
                                                  new WorkDaySpan()
                                                      .Time(new TimeSpan(9, 0, 0), new TimeSpan(13, 0, 0))
                                                      .Time(new TimeSpan(14, 0, 0), new TimeSpan(18, 0, 0)))
                           )
                      .Holiday(new AHolyDay(15, 8))
                      .Holiday(2, 6)
                      .Holiday(new EasterMonday());

var cfg2 = new WorkingDaysConfig().Week(WeekDaySpan.CreateSymmetricalConfig(new WorkDaySpan()
																			.Time(new TimeSpan(9, 0, 0),
																					new TimeSpan(13, 0, 0))
																			.Time(new TimeSpan(14, 0, 0),
																					new TimeSpan(18, 0, 0)),
																			new DayOfWeek[]
																			{
																				DayOfWeek.Monday,
																				DayOfWeek.Tuesday,
																				DayOfWeek.Wednesday,
																				DayOfWeek.Thursday,
																				DayOfWeek.Friday
																			}));

License

This software is licensed by BSD-3-Clause. Link to License: license

Product Versions
.NET net5.0 net5.0-windows net6.0 net6.0-android net6.0-ios net6.0-maccatalyst net6.0-macos net6.0-tvos net6.0-windows net7.0 net7.0-android net7.0-ios net7.0-maccatalyst net7.0-macos net7.0-tvos net7.0-windows
.NET Core netcoreapp2.0 netcoreapp2.1 netcoreapp2.2 netcoreapp3.0 netcoreapp3.1
.NET Standard netstandard2.0 netstandard2.1
.NET Framework net461 net462 net463 net47 net471 net472 net48 net481
MonoAndroid monoandroid
MonoMac monomac
MonoTouch monotouch
Tizen tizen40 tizen60
Xamarin.iOS xamarinios
Xamarin.Mac xamarinmac
Xamarin.TVOS xamarintvos
Xamarin.WatchOS xamarinwatchos
Compatible target framework(s)
Additional computed target framework(s)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.0.12 145 1/19/2023
2.0.11 1,035 6/27/2022
2.0.10 2,826 8/6/2021
2.0.9 237 8/2/2021
2.0.8 250 7/14/2021
2.0.7 256 7/13/2021
2.0.6 11,098 3/30/2021
2.0.5 11,051 6/29/2020
2.0.4 12,253 11/15/2018
2.0.3 550 11/15/2018
2.0.2 571 11/15/2018
2.0.1 574 11/14/2018
2.0.0 587 11/13/2018

Fix errors on serializing and de-serializing List of AHolyDay