DateRange 1.0.3

dotnet add package DateRange --version 1.0.3                
NuGet\Install-Package DateRange -Version 1.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="DateRange" Version="1.0.3" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add DateRange --version 1.0.3                
#r "nuget: DateRange, 1.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 DateRange as a Cake Addin
#addin nuget:?package=DateRange&version=1.0.3

// Install DateRange as a Cake Tool
#tool nuget:?package=DateRange&version=1.0.3                

DateTimeRange

NuGet NuGet

Provides a DateTimeRange type with enumerator for better handling of DateTime ranges in .NET.

Add Nuget-Package

You should install DateRange with NuGet:

Install-Package DateRange

Or via the .NET Core command line interface:

dotnet add package DateRange

Either commands, from Package Manager Console or .NET Core CLI, will download and install DateRange. DateRange is compatible with .NET 4.6.1+, NET Standard, NET Core and all .NET versions above.

Usage

Import System:

  using System;

Initialize:

  //With start and end date
  var b = new DateTimeRange(DateTime.Now, DateTime.Now.AddMonths(3));
  
  //With start date and duration
  var c = new DateTimeRange(DateTime.Now, new TimeSpan(100, 0, 0, 0));
  
  //With end date and duration
  var d = new DateTimeRange(new TimeSpan(100, 0, 0, 0), DateTime.Now);

Enumerate:

Enumerate DateTimes inside of the DateTimeRange in custom steps. StartDate and EndDate are optionally excludable.

  //Using the custom DateSpan Enum
  DateSpan dateSpan = DateSpan.Day;
  var result = dateTimeRange.Enumerate(dateSpan).ToArray();
  var result = dateTimeRange.Enumerate(dateSpan, excludeStart: true, excludeEnd: true).ToArray();
  
  //Using TimeSpan
  TimeSpan step = new TimeSpan(1,0,0,0);
  var result = dateTimeRange.Enumerate(step).ToArray();
  var result = dateTimeRange.Enumerate(step, excludeStart: true, excludeEnd: true).ToArray();

Contains:

Check if a DateTime is inside of a DateTimeRange. StartDate and EndDate are optionally excludable.

  // Declare DateTimeRange and DateTime to check for
  DateTimeRange dateTimeRange = new DateTimeRange(DateTime.Now, DateTime.Now.AddDays(2));
  DateTime dateTime = DateTime.Now.AddDay(1);

  // Check if DateTime is inside of DateTimeRange
  var result = dateTimeRange.Contains(dateTime);
  var result = dateTimeRange.Contains(dateTime, excludeStart: true, excludeEnd: true);

  Assert.That(result);

Intersect:

Combine two DateTimeRanges into one. The new DateTimeRange will range from the earliest StartDate to the latest EndDate.

  //Declare two DateTimeRanges
  var a = DateTime.Now;
  var b = a.AddDays(-1);
  var c = a.AddDays(1);
  DateTimeRange first = new DateTimeRange(b, a);
  DateTimeRange second = new DateTimeRange(a, c);

  //Intersect
  var result = first.Intersect(second);

  Assert.That(result.Start == b && result.End == c);

P.S.:

Let me know, if you like to have more features.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.0

    • No dependencies.

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
1.0.3 34 1/24/2025
1.0.2 101,346 1/11/2021
1.0.1 352 1/11/2021
1.0.0 528 5/15/2020