DateOnly and TimeOnly in .NET 6

In this post, we will look at the DateOnly and TimeOnly types introduced in the.NET 6 core library for representing dates and times independently.

DateOnly and TimeOnly in .NET 6

Two long-awaited types have been added to the core library in.NET 6 (preview 4). Developers can use DateOnly and TimeOnly to express either the date or time element of a DateTime. These two new types are structs (value types) and can be used when your code has to deal with a date or time notions separately. The System namespace contains both types. Using these new types may be consistent with how databases allow for the representation of similar data. These types, in particular, are compatible with the SQL Server date and time data types.

Using DateOnly in .NET 6

In terms of what they represent, the types are very self-explanatory. We may use DateOnly to express a date without a time component. For example, we may represent someone's date of birth in our application. In such instances, we seldom need to use the time component of a DateTime, and a conventional approach would be to set the time to 00:00:00.000. We may be more specific about our intentions when using DateOnly.

We may create an instance of DateOnly by supplying the year, month, and day as arguments:

var date = new DateOnly(2021, 06, 04);

This creates a DateOnly that represents 4th June 2021.

If we want to create a DateOnly instance from the existing DateTime, it can be achieved by calling the FromDateTime method.

var currentDate = DateOnly.FromDateTime(DateTime.Now);

This creates a DateOnly that represents 4th June 2021.

If we want to create a DateOnly instance from the existing DateTime, it can be achieved by calling the FromDateTime method.

var currentDate = DateOnly.FromDateTime(DateTime.Now);

We may parse a string expressing a date into a DateOnly form using either Parse, which may cause an exception, or TryParse, which returns a bool indicating success or failure, much like we can with the existing DateTime type.

if (DateOnly.TryParse("04/06/2021", new CultureInfo("en-US"), DateTimeStyles.None, out var result))

{

    Console.WriteLine(result);

}

We can also add days, months, or years to a DateOnly instance, which will result in a new instance with the updated date.

var newDate = date.AddDays(1).AddMonths(1).AddYears(1);

USING TIMEONLY IN .NET 6

The TimeOnly struct is used to represent a time that is not related to a date. Consider developing a reminder app that will remind the users about the specific task every day. In this case, the date part is not important as we want to remember the time of the day when the reminder should pop up.

There are various constructor overloads for the TimeOnly type. The more popular ones, which I expect most developers to use, allow us to create a date that accepts either the hour and minute, the hour, minute, and second, or the hour, minute, second, and millisecond for the time.

public TimeOnly(int hour, int minute)

public TimeOnly(int hour, int minute, int second)

public TimeOnly(int hour, int minute, int second, int millisecond)

we can represent 08:30 am by creating the following TimeOnly instance.

var startTime = new TimeOnly(08, 30);

The hour portion is expected to be provided using the 24-hour clock format, where 1 pm is 13 hours.

TimeOnly internally holds a long that represents the number of ticks (100 nanosecond intervals) that have passed since midnight by the specified time. For example, 1 a.m. is one hour into the day, which is 36,000,000,000 ticks since midnight (00:00:00.0000000). we can construct a TimeOnly by providing the ticks as an input.

public TimeOnly(long ticks);
var endTime = new TimeOnly(14, 00, 00);

We can also perform mathematical operations on these TimeOnly instances, such as calculating the difference.

var diff = endTime - startTime;

I hope you learned something new from this article. If you have any comments or suggestions, please leave them behind in the comments section below. Do not forget to share this article within your developer community. Thanks and Happy Coding!