DateOnly And TimeOnly In C#

Introduction

We have been using the DateTime struct to fulfill both requirements for the longest time. Even if we build up a whole DateTime instance, we typically just care about the date component or the time component, but very often both, when it comes to DateTime.

Developers eagerly awaited the introduction of two new types, DateOnly and TimeOnly, which were included in.NET 6 and C# 10. Each one separates the date and time.

In some cases, we are forced to consider the time component of the birthdate even though we presumably don't care about it. Additionally, our database likely has the field set to datetime, which obtrusively persists in the time component.

Example

var dob= new DateTime(1995, 8, 19);
database.Save(dob);

This indicates that we frequently struggle with storage and unneeded complexity. For instance, SQL Server already has distinct date and time types, which we couldn't use.

Now that this issue has been resolved, to address it, two new types of data called DateOnly and TimeOnly were added to NET 6/C# 10.

DateOnly in C#

When we only wish to represent the date component, we can use the new DateOnly struct. A historical recording, where we are more interested in the fact of the event than the exact moment it occurred, such as a person's date of birth, maybe a good example.

Syntax

var Var_Name  = new DateOnly(Year, Month, Day);

Example

var dateOnly = new DateOnly(1995, 8, 19);

Examine the difference between the normal DateTime and DateOnly types when printed. 

var dateOnly = new DateOnly(1995, 8, 19);
var dateTime = new DateTime(1995, 8, 19);

We'll get different outcomes if we print them both out.

19/08/1995
19/08/1995 12:00:00 AM

Perform some operations on DateOnly Variable

var dateOnly = new DateOnly(1995,08,19);
Console. WriteLine(dateOnly);
var addDays = dateOnly.AddDays(1);
Console. WriteLine(addDays);
var addMonths = dateOnly.AddMonths(1);
Console. WriteLine(addMonths);
var addYears = dateOnly.AddYears(1);
Console. WriteLine(addYears);

Output

19/08/1995  //Original Date
20/08/1995  //Add 1 Day
19/09/1995  //Add 1 Month
19/08/1996  //Add 1 Year

TimeOnly in C#

The new TimeOnly struct can be used when we are just concerned with the time component. A good illustration of this is when an event is repeated every day at 9:00 AM.

Syntax

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

Example

var eventTime = new TimeOnly(9,35);

When we print above the variable, it looks like this: 

09:35 AM

Perform some operations on TimeOnly Variable

var timeOnly = new TimeOnly(9,35);
Console. WriteLine(timeOnly);
var addHours = timeOnly.AddHours(2);
Console. WriteLine(addHours);
var addMinutes = timeOnly.AddMinutes(5);
Console. WriteLine(addMinutes);

Output

09:35 AM  //Original Time
11:35 AM  //Add 2 Hours
09:40 AM  //Add 5 Minutes

Summary

The new DateOnly and TimeOnly types added to.NET 6 were discussed in this article. In.NET, we now have greater alternatives for handling dates and timings, giving us more freedom to make choices.