Hto3.DateTimeHelpers 1.1.0

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

// Install Hto3.DateTimeHelpers as a Cake Tool
#tool nuget:?package=Hto3.DateTimeHelpers&version=1.1.0                

logo

Hto3.DateTimeHelpers

License Hto3.DateTimeHelpers Downloads Build Status codecov Codacy Badge

Features

A set of extension methods that can be used to manipulate date and time solving common dev problems.

UnixTimeToUTCDateTime

Convert unix timestamp to UTC DateTime

1544572230L.UnixTimeToUTCDateTime() == new DateTime(2018, 12, 11, 23, 50, 30, DateTimeKind.Utc);

UnixTimeToDateTime

Convert unix timestamp to DateTime

1544572230L.UnixTimeToDateTime() == new DateTime(2018, 12, 11, 21, 50, 30); //When local timezone is -02:00 GMT

ToUnixTime

Convert a DateTime to unix timestamp

new DateTime(2018, 12, 11, 21, 50, 30).ToUnixTime() == 1544572230L; //When local timezone is -02:00 GMT

StripTime

Strip the time part of a DateTime

new DateTime(2018, 12, 11, 21, 50, 30).StripTime() == new DateTime(2018, 12, 11);

EachDay

Gets the dates between date range

var start = new DateTime(2018, 2, 26);
var end = new DateTime(2018, 3, 4);

var result = start.EachDay(end).ToArray();

result[0] == new DateTime(2018, 2, 26);
result[1] == new DateTime(2018, 2, 27);
result[2] == new DateTime(2018, 2, 28);
result[3] == new DateTime(2018, 3, 1);
result[4] == new DateTime(2018, 3, 2);
result[5] == new DateTime(2018, 3, 3);
result[6] == new DateTime(2018, 3, 4);

NextMonth

Get the first day of the next month of a base date. This is the same as calling .AddMonths(1).

new DateTime(2018, 3, 4).NextYear() == new DateTime(2019, 4, 4);

NextYear

Get the first day of the next year of a base date. This is the same as calling .AddYears(1).

new DateTime(2018, 3, 4).NextYear() == new DateTime(2019, 3, 4);

NextDay

Get the next day of a base date. This is the same as calling .AddDays(1).

new DateTime(2018, 3, 4).NextDay() == new DateTime(2018, 3, 5);

NextWeek

Get the next week of a base date. This is the same as calling .AddWeeks(1).

new DateTime(2018, 3, 4).NextWeek() == new DateTime(2018, 3, 11);

NextHour

Get the next hour of a base DateTime. This is the same as calling .AddHours(1).

new DateTime(2018, 12, 11, 21, 50, 30).NextHour() == new DateTime(2018, 12, 11, 22, 50, 30);

NextMinute

Get the next minute of a base DateTime. This is the same as calling .AddMinutes(1).

new DateTime(2018, 12, 11, 21, 50, 30).NextMinute() == new DateTime(2018, 12, 11, 21, 51, 30);

NextSecond

Get the next second of a base DateTime. This is the same as calling .AddSeconds(1).

new DateTime(2018, 12, 11, 21, 50, 30).NextSecond() == new DateTime(2018, 12, 11, 21, 50, 31);

PreviousYear

Get the first day of the previous year of a base date. This is the same as calling .AddYears(-1).

new DateTime(2018, 12, 11, 21, 50, 30).PreviousYear() == new DateTime(2017, 12, 11, 21, 50, 30);

PreviousMonth

Get the first day of the previous month of a base date. This is the same as calling .AddMonths(-1).

new DateTime(2018, 12, 11, 21, 50, 30).PreviousMonth() == new DateTime(2018, 11, 11, 21, 50, 30);

PreviousDay

Get the previous day of a base date. This is the same as calling .AddDays(-1).

new DateTime(2018, 12, 11, 21, 50, 30).PreviousDay() == new DateTime(2018, 12, 10, 21, 50, 30);

PreviousWeek

Get the previous week of a base DateTime. This is the same as calling .AddWeeks(-1).

new DateTime(2018, 12, 11, 21, 50, 30).PreviousWeek() == new DateTime(2018, 12, 8, 21, 50, 30);

PreviousHour

Get the previous hour of a base DateTime. This is the same as calling .AddHours(-1).

new DateTime(2018, 12, 11, 21, 50, 30).PreviousHour() == new DateTime(2018, 12, 11, 20, 50, 30);

PreviousMinute

Get the previous minute of a base DateTime. This is the same as calling .AddMinutes(-1).

new DateTime(2018, 12, 11, 21, 50, 30).PreviousMinute() == new DateTime(2018, 12, 11, 21, 49, 30);

PreviousSecond

Get the previous second of a base DateTime. This is the same as calling .AddSeconds(-1).

new DateTime(2018, 12, 11, 21, 50, 30).PreviousSecond() == new DateTime(2018, 12, 11, 21, 50, 29);

Midnight

Returns a new datetime with the same date but at midnight.

new DateTime(2018, 12, 11, 21, 50, 30).Midnight() == new DateTime(2018, 12, 11, 0, 0, 0);

Noon

Returns a new datetime with the same date but at noon.

new DateTime(2018, 12, 11, 21, 50, 30).Noon() == new DateTime(2018, 12, 11, 12, 0, 0);

SetYear

Change the year of a DateTime.

new DateTime(2018, 12, 11, 21, 50, 30).SetYear(1999) == new DateTime(1999, 12, 11, 21, 50, 30);

SetMonth

Change the month of a DateTime.

new DateTime(2018, 12, 11, 21, 50, 30).SetMonth(10) == new DateTime(2018, 10, 11, 21, 50, 30);

SetDay

Change the day of a DateTime.

new DateTime(2018, 12, 11, 21, 50, 30).SetDay(27) == new DateTime(2018, 12, 27, 21, 50, 30);

SetHour

Change the hour of a DateTime.

new DateTime(2018, 12, 11, 21, 50, 30).SetHour(1) == new DateTime(2018, 12, 11, 1, 50, 30);

SetMinute

Change the minute of a DateTime.

new DateTime(2018, 12, 11, 21, 50, 30).SetMinute(9) == new DateTime(2018, 12, 11, 9, 50, 30);

SetSecond

Change the second of a DateTime.

new DateTime(2018, 12, 11, 21, 50, 30).SetSecond(0) == new DateTime(2018, 12, 11, 21, 50, 0);

SetMillisecond

Change the milisecond of a DateTime.

new DateTime(2018, 12, 11, 21, 50, 30, 3).SetMillisecond(9) == new DateTime(2018, 12, 11, 21, 50, 30, 9);

GetMonthWeeks

Get a collection of weeks of a month.

var weeks = DateTimeHelpers.GetMonthWeeks(2, 2018);

weeks.ElementAt(0).Item1 == new DateTime(2018, 2, 1);//week start
weeks.ElementAt(0).Item2 == new DateTime(2018, 2, 3);//week end

weeks.ElementAt(1).Item1 == new DateTime(2018, 2, 4);//week start
weeks.ElementAt(1).Item2 == new DateTime(2018, 2, 10);//week end

weeks.ElementAt(2).Item1 == new DateTime(2018, 2, 11);//week start
weeks.ElementAt(2).Item2 == new DateTime(2018, 2, 17);//week end

weeks.ElementAt(3).Item1 == new DateTime(2018, 2, 18);//week start
weeks.ElementAt(3).Item2 == new DateTime(2018, 2, 24);//week end

weeks.ElementAt(4).Item1 == new DateTime(2018, 2, 25);//week start
weeks.ElementAt(4).Item2 == new DateTime(2018, 2, 28);//week end

AddWeeks

Add weeks.

new DateTime(2018, 10, 1).AddWeeks(1) == new DateTime(2018, 10, 8);

DateDiff

Calculate the difference between two dates.

var start = new DateTime(2010, 1, 1);
var end = new DateTime(2010, 1, 2);
start.DateDiff(end) == TimeSpan.FromDays(1);

CalculateAge

Calculate the age in years based on the birthday.

//assuming today as 02/26/2019
new DateTime(2018, 2, 25).CalculateAge() == 1;

IsBetween

Verify if the date is between a range of dates.

var start = new DateTime(2009, 1, 1);
var end = new DateTime(2010, 1, 2);
new DateTime(2010, 1, 1).IsBetween(start, end) == true;

FirstDayOfMonth

Return the date of the first day in the specified month.

new DateTime(2018, 2, 26).FirstDayOfMonth() == new DateTime(2018, 2, 1);

AmountDaysInMonth

Return the amount of days in the month.

//Date used only to extract the year and month
new DateTime(2018, 1, 1).AmountDaysInMonth() == 31;

LastDayOfMonth

Return a new DateTime on the last day of the month based on the informed DateTime.

//Date used only to extract the year and month
new DateTime(2018, 1, 1).AmountDaysInMonth() == 31;

IsValidDate

Return true if the numbers representing the year, month and day build a valid date.

DateTimeHelpers.IsValidDate(2010, 1, 20) == true;
DateTimeHelpers.IsValidDate(99, -1, 99) == false;

IsValidTime

Return true if the numbers representing the hour, minute and second of a time build a valid time.

DateTimeHelpers.IsValidTime(23, 59, 1) == true;
DateTimeHelpers.IsValidTime(24, -1, 0) == false;

NextFullOddHour

Return the next odd full hour. Example: Now it's 09/09/2018 15:36:12 so the next odd full hour will be 09/09/2018 17:00:00.

var dateAndTime = new DateTime(2010, 10, 3, 12, 22, 7);
var expected = new DateTime(2010, 10, 3, 13, 0, 0);
dateAndTime.NextFullOddHour() == expected;

NextFullEvenHour

Return the next even full hour. Example: Now it's 09/09/2018 15:36:12 so the next even full hour will be 09/09/2018 16:00:00.

var dateAndTime = new DateTime(2010, 10, 3, 12, 22, 7);
var expected = new DateTime(2010, 10, 3, 14, 0, 0);
dateAndTime.NextFullOddHour() == expected;

NextFullHour

Return the next full hour. Example: Now it's 09/09/2018 15:36:12 so the next half hour will be 09/09/2018 16:00:00.

var dateAndTime = new DateTime(2010, 10, 3, 12, 22, 7);
var expected = new DateTime(2010, 10, 3, 13, 0, 0);
dateAndTime.NextFullOddHour() == expected;

NextHalfHour

Return the next half hour. Example: Now it's 09/09/2018 15:36:12 so the next half hour will be 09/09/2018 16:00:00.

var dateAndTime = new DateTime(2010, 10, 3, 12, 22, 7);
var expected = new DateTime(2010, 10, 3, 12, 30, 0);
dateAndTime.NextFullOddHour() == expected;
Product Compatible and additional computed target framework versions.
.NET net5.0 is compatible.  net5.0-windows was computed.  net6.0 is compatible.  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 is compatible.  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 is compatible.  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. 
.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 is compatible. 
.NET Framework net461 is compatible.  net462 was computed.  net463 was computed.  net47 is compatible.  net471 was computed.  net472 was computed.  net48 is compatible.  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.
  • .NETFramework 4.6.1

    • No dependencies.
  • .NETFramework 4.7

    • No dependencies.
  • .NETFramework 4.8

    • No dependencies.
  • .NETStandard 2.0

    • No dependencies.
  • .NETStandard 2.1

    • No dependencies.
  • net5.0

    • No dependencies.
  • net6.0

    • No dependencies.
  • net7.0

    • No dependencies.
  • net8.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.1.0 181 1/7/2024
1.0.9 169 5/15/2023
1.0.8 350 11/15/2021
1.0.7 349 2/17/2021
1.0.6 437 10/18/2020
1.0.5 510 1/21/2020
1.0.4 526 9/28/2019
1.0.3 533 6/15/2019
1.0.2 663 1/5/2019
1.0.1 630 12/23/2018
1.0.0 617 12/23/2018