Coding Faster with the dotNetTips Utility: DateTime Extensions

If you talk to any seasoned developer, they will usually say that dealing with dates and times is a real pain. While it has gotten better since .NET was first released, it could always be better. For this article, I will be showing the extension methods that are in my open-source project, dotNetTips.Utility.Standard.Extensions  for the DateTime and DateTimeOffset types. This project is also a NuGet package too. Most of the examples below work for both the DateTime and DateTimeOffset types. All the code examples are from the unit test project for the assembly.

Before I show you the code, I’d like to say, “Stop using DateTime!”. Any new code should use DateTimeOffset (as Microsoft recommends) so it can properly handle time zone information. DateTimeOffset can also be used with SQL Server. You should also strive to change older projects to use this new type too. Though that will be harder to implement. Make sure you have plenty of unit tests before changing to DateTimeOffset.

Looking For The Previous Day of the Week

If you want to find a previous day of the week, you can use the GetLast() extension method below with the DayOfWeek enumeration.

var result = DateTime.Now.GetLast(DayOfWeek.Monday);

Output

Here is the result of the code above using the current date of Friday, 7/17/2020.

7/13/2020 11:24:46 AM

Looking For The Next Day of the Week

If you want to find the next day of the week, you can use the GetNextDayOfWeek() extension method below with the DayOfWeek enumeration.

var result = DateTime.Now.GetNextDayOfWeek(DayOfWeek.Monday);

Output

Here is the result of the code above using the current date of Friday, 7/17/2020.

7/20/2020 11:24:46 AM

Looking For a Date that Intersects a Date Range

If you want to find if a date intersects a date range, you can use the Intersects() extension method below.

var now = DateTime.Now;
var result = now.Intersects(endDate: now.AddDays(100), 
                            intersectingStartDate: now.AddDays(1), 
                            intersectingEndDate: now.AddDays(10));

Looking For a Date in a Date Range

If you want to find if a date is within a given date range, you can use the IsInRange() extension method below.

var now = DateTime.Now;
var result = now.IsInRange(beginningTime: 
                 now.Subtract(new TimeSpan(1, 0, 0, 0)), 
                 endTime: now.AddDays(10));

Converting a Date using Time Zone Offset

If you want to convert local time to UTC using the time zone offset number, you can use the LocalTimeFromUtc() extension method below.

var now = DateTime.Now;
var result = now.LocalTimeFromUtc(-5);

Output

Here is the result of the code above using the current date of Friday, 7/17/2020 11:55:24.

7/17/2020 1:55:24 PM

Looking for the Max Date

If you want to convert local time to UTC using the time zone offset number, you can use the Max() extension method below.

var now = DateTime.Now;

var result = now.Max(now.Subtract(new TimeSpan(1, 0, 0, 0)));

Creating a Friendly Date String

If you want to get a friendly date string to show to users, you can use the ToFriendlyDateString() extension method below.

var result = DateTime.Now.ToFriendlyDateString();

Output

Here is the result of the code above.

Today @ 8:31:01 am

Summary

I hope you will try these DateTime and DateTimeOffset extension methods in your code. There are more extension methods in this class that you can use too. They will help you deal with dates better, just like they do for me and the projects I work on. Just install the NuGet package and you are ready to go! How do you like working with dates and times? Please comment below.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.