C# 11 Dates and Times

It is a rare application that can be developed without in some way needing to work with dates and times. Recognizing this, the Microsoft engineers responsible for C# gave us the DateTime object. This chapter will look at using this object to work with dates and times in C# based applications. Topics covered include adding and subtracting time, getting the system date and time, and formatting and extracting elements of dates and times in C#.

Creating a C# DateTime object

The first step in using the DateTime object when working with dates and times in C# is to create an object instance. You do this using the new keyword passing through year, month, and day values. For example, to create a DateTime object preset to April 9, 2021, you would write the following code:

DateTime meetingAppt = new DateTime(2023, 4, 9);
System.Console.WriteLine (meetingAppt.ToString());

Output:

04/09/2023 00:00:00

In the above example, after setting the date, we use the ToString() method of the DateTime object to output the current date and time value as a string.

Note: If a time is not specified along with the date, the DateTime class constructor will set the time to 12:00 am.

Time values are specified by passing through hours, minutes, and seconds values to the constructor. The following code, for example, sets the time property of a DateTime object to 14:30:00 using the same date as before:

DateTime meetingAppt = new DateTime(2023, 4, 9, 14, 30, 0);
System.Console.WriteLine (meetingAppt.ToString());

Output:

04/09/2023 14:30:00

Getting the current system time and date

The system date and time of the computer on which the C# code is executing may be accessed using the Today and Now static properties of the DateTime class. The Today property will return the current system date (and the time set to 12:00 AM), while the Now property returns the current date and time. For example, the following code demonstrates these properties (keeping in mind that the server on which the code is executing may not be in your timezone):

System.Console.WriteLine(DateTime.Today.ToString());
System.Console.WriteLine(DateTime.Now.ToString());

Output:

03/07/2023 00:00:00
03/07/2023 19:52:30

Adding or subtracting from dates and times

The C# DateTime object provides several methods for adding or subtracting dates and times from a DateTime object instance. These methods are outlined in the following table:

MethodDescription
AddAdds/Subtracts the value of the specified TimeSpan object instance.
AddDaysAdds/Subtracts the specified number of days.
AddHoursAdds/Subtracts the specified number of hours.
AddMillisecondsAdds/Subtracts the specified number of Milliseconds.
AddMinutesAdds/Subtracts the specified number of minutes.
AddMonthsAdds/Subtracts the specified number of months.
AddSecondsAdds/Subtracts the specified number of seconds.
AddYearsAdds/Subtracts the specified number of years.
Table 1-1

A vital issue to understand is that these methods do not change the value of the DateTime object on which the method is called but instead return a new DateTime object primed with the modified date and time. For example, add five days to our example as follows:

DateTime meetingAppt = new DateTime(2023, 4, 9, 14, 30, 0);
DateTime newAppt = meetingAppt.AddDays(5);
System.Console.WriteLine (newAppt.ToString());

Output:

04/14/2023 14:30:00

To subtract from a date and time, pass through a negative value to the appropriate method. For example, we can subtract ten months from our example object as follows:

DateTime meetingAppt = new DateTime(2023, 4, 9, 14, 30, 0);
DateTime newAppt = meetingAppt.AddMonths(-10);
System.Console.WriteLine (newAppt.ToString());

Output:

06/09/2022 14:30:00

Retrieving parts of a date and time

Dates and times comprise distinct and separate values, namely the day, month, year, hours, minutes, seconds, and milliseconds. The C# DateTime object stores each value as a separate property within the object, allowing each to be accessed individually. For example, the following code sample extracts each value and displays it in the console:

DateTime meetingAppt = new DateTime(2023, 4, 9, 14, 30, 0);

System.Console.WriteLine(meetingAppt.Day);
System.Console.WriteLine(meetingAppt.Month);
System.Console.WriteLine(meetingAppt.Year);
System.Console.WriteLine(meetingAppt.Hour);
System.Console.WriteLine(meetingAppt.Minute);
System.Console.WriteLine(meetingAppt.Second);
System.Console.WriteLine(meetingAppt.Millisecond);

Output:

9
4
2023
14
30
0
0

Categories