When we need to work with time in C# we use the DateTime type. DateTime values have two components, as easily inferred from the name – a date part and a time part. Since in some cases we might need only one part out of the two, in this article we’re going to learn how to remove time from a DateTime in C#.
Let’s dive in.
Remove Time Using the Built-in Date Property
C# offers some useful built-in properties to each DateTime
value. To get the Date
component out of a DateTime
, we simply need to access it by the .Date
property. This property returns a new DateTime
object, containing the original Date
component, and a default hour of 12 at midnight (00:00:00):
var date1 = new DateTime(2022, 02, 14, 10, 40, 00); var date2 = new DateTime(2018, 10, 18, 11, 23, 34); Console.WriteLine("Full Date:"); Console.WriteLine(date1.ToString()); //2/14/2022 10:40:00 AM Console.WriteLine(date2.ToString()); //10/18/2018 11:23:34 AM Console.WriteLine("Only .Date part:"); Console.WriteLine(date1.Date.ToString()); //2/14/2022 12:00:00 AM Console.WriteLine(date2.Date.ToString()); //10/18/2018 12:00:00 AM
We get the same Date values, but for the time part, we get the default of 00:00:00. In other words, we got the Date component by simply erasing the values about time.
Format DateTime Using the ToString() Method
Besides extracting the date we can also choose not to show it. So, if simply not displaying the time part is enough, we can do this easily. DateTime
offers flexible ways to format data for display purposes.
We can use a full range of patterns as we prefer, they can all be added in the ToString()
method applied to a DateTime
:
var date1 = new DateTime(2022, 02, 14, 10, 40, 00); var date2 = new DateTime(2018, 10, 18, 11, 23, 34); Console.WriteLine("Hide the time part:"); Console.WriteLine(date1.Date.ToString("MM/dd/yyyy")); //02/14/2022 Console.WriteLine(date2.Date.ToString("dd/MM/yyyy")); //18/10/2018
The MM
stands for the month number in two digits, the dd
stands for the day number in two digits, and the yyyy
stands for the year, expressed in 4 digits.
There are also patterns available for showing the time components, to very fine details – hour, minute second, or second fraction. But our goal today is to hide the time, and that’s what we’ve done.
We can play and reorder these date components however we please.
Using the ToShortDateString() Method
Another easy to use built-in method to display the date part is ToShortDateString()
. This method converts the current DateTime
value into a short representation as a string. The result includes the day, month, and year values:
var date3 = new DateTime(2022, 02, 14, 10, 40, 00); Console.WriteLine("Short Date Value:"); Console.WriteLine(date3.Date.ToShortDateString()); //2/14/2022
Remove Time Using the Built-in Type DateOnly
Starting with .NET 6, we can make use of the DateOnly type. We can create aDateOnly
from scratch, or we can create it from an existing DateTime
. To create a DateOnly
we need the three values for day, month, and year. We can simply copy them from an existing DateTime
value and keep working with the newly created DateOnly
object:
var date = new DateTime(2021, 7, 8, 11, 10, 9); var dateOnly = new DateOnly(date.Year, date.Month, date.Day); Console.WriteLine(dateOnly); //7/8/2021
Remove Time Using String.Format() Method
The String.Format()
is a very powerful and flexible method in C#. It’s usable with multiple data types. We can pass one of many formats to decide how we display our values. For DateTime
, the standard formats we can use to show only the date part are "d"
for the short date pattern, or "D"
for long date pattern:
var may12 = new DateTime(2022, 5, 12, 10, 15, 11); Console.Write(String.Format($"{may12:d}")); //5/12/2022 Console.Write(String.Format($"{may12:D}")); //Thursday, May 12, 2022
And besides these quick, standard formats, we can also customize the format however we see convenient. The components we accessed in the previous option with ToString()
are also available here.
The dd
can be used to ask for the day of the month expressed in 2 digits. The day of the week can be accessed with ddd
. We can get the month expressed in 2 digits with MM
, in three letters with MMM
, or with its full name using MMMM
. The year is most commonly expressed either in two digits with yy
, or in 4 digits, with yyyy
.
Let’s remove time from a DateTime
object using the String.Format()
method:
var april10 = new DateTime(2022, 4, 10, 10, 15, 11); Console.WriteLine(String.Format($"{april10:dd-MM-yy}")); //10-04-22 Console.WriteLine(String.Format($"{april10:ddd, dd MM yy}")); //Sun, 10 04 22 Console.WriteLine(String.Format($"{april10:dd MMM yyyy}")); //10 Apr 2022 Console.WriteLine(String.Format($"{april10:ddd, dd MMMM, yyyy}")); //Sun, 10 April, 2022
Notice how we can use the separators we prefer, such as dashes, commas, or simply, spaces. This method offers a lot of flexibility on how we get the date part out of a DateTime
.
Conclusion
There are several different ways to remove time from a DateTime object in C#. In this article, we’ve covered the most used and the most flexible ways we can do that.