Convert DateTime to date in C#

HomeC#Featured

Convert DateTime to date in C#

Switch operator in C#
Unleash the Power of Now: Mastering Asynchronous Programming in C#
Open a database connection asynchronously

You can convert a DateTime to a DateOnly (or just get the date portion if you’re using older .NET versions without DateOnly) in C# using several methods. Here’s a detailed explanation with examples:

1. Using DateOnly (for .NET 6 and later):

The DateOnly struct represents a date without a time component. This is the most straightforward and recommended approach if you’re using a compatible .NET version.

DateTime dateTime = new DateTime(2024, 10, 27, 10, 30, 0); // October 27, 2024, 10:30 AM

// Convert DateTime to DateOnly
DateOnly dateOnly = DateOnly.FromDateTime(dateTime);

Console.WriteLine(dateOnly); // Output: 10/27/2024 (or the format based on your culture)

// Or, you can construct a DateOnly directly from year, month, and day:
DateOnly anotherDateOnly = new DateOnly(2024, 10, 27);
Console.WriteLine(anotherDateOnly); // Output: 10/27/2024

// You can also get a DateOnly from the DateTime.Today property.
DateOnly today = DateOnly.FromDateTime(DateTime.Today);
Console.WriteLine(today); // Output: The current date.

// You can compare DateOnly values directly:
if (dateOnly == anotherDateOnly) {
    Console.WriteLine("The dates are equal."); // This will be printed.
}

// Formatting DateOnly:
string formattedDate = dateOnly.ToString("yyyy-MM-dd"); // ISO 8601 format
Console.WriteLine(formattedDate); // Output: 2024-10-27

2. Using DateTime.Date property (for all .NET versions):

This property returns a new DateTime object with the same date as the original but with the time set to midnight (00:00:00). It’s useful if you’re working with older .NET versions or if you need to keep a DateTime type but just want to discard the time part.

DateTime dateTime = new DateTime(2024, 10, 27, 10, 30, 0);

// Get the date portion as a new DateTime
DateTime dateOnlyDateTime = dateTime.Date;

Console.WriteLine(dateOnlyDateTime); // Output: 10/27/2024 00:00:00

// Formatting the DateTime:
string formattedDate = dateOnlyDateTime.ToString("yyyy-MM-dd"); // ISO 8601 format
Console.WriteLine(formattedDate); // Output: 2024-10-27

// Comparing dates (note that you're technically comparing DateTimes at midnight):
DateTime anotherDateTime = new DateTime(2024, 10, 27, 14, 0, 0);
if (dateTime.Date == anotherDateTime.Date)
{
    Console.WriteLine("The dates are equal."); // This will be printed.
}

3. String Conversion (less recommended):

You can convert the DateTime to a string with a date-only format and then parse it back into a DateTime or DateOnly. However, this approach is generally less efficient and can be prone to culture-specific formatting issues. It’s best to avoid this method if possible.

DateTime dateTime = new DateTime(2024, 10, 27, 10, 30, 0);

// Convert to string with date-only format
string dateString = dateTime.ToString("yyyy-MM-dd");

// Parse back to DateTime
DateTime parsedDateTime = DateTime.Parse(dateString);
Console.WriteLine(parsedDateTime); // Output: 10/27/2024 00:00:00

// Parse back to DateOnly (requires .NET 6 or later)
DateOnly parsedDateOnly = DateOnly.Parse(dateString);
Console.WriteLine(parsedDateOnly); // Output: 10/27/2024

Which method should you use?

  • For .NET 6 and later, use DateOnly.FromDateTime() or construct a DateOnly directly. This is the cleanest and most efficient approach for working with dates without times.
  • If you’re using an older .NET version or need to maintain a DateTime type but just want to set the time to midnight, use the DateTime.Date property.
  • Avoid string conversion unless it’s absolutely necessary.

Example: Filtering by Date:

Let’s say you have a list of DateTime values and you want to filter them to only include entries from a specific date.

List<DateTime> appointments = new List<DateTime>
{
    new DateTime(2024, 10, 27, 10, 0, 0),
    new DateTime(2024, 10, 27, 14, 0, 0),
    new DateTime(2024, 10, 28, 9, 0, 0),
    new DateTime(2024, 10, 27, 11, 0, 0)
};

DateOnly targetDate = new DateOnly(2024, 10, 27); // DateOnly approach

//Or if you're using an older version of .NET:
DateTime targetDateOld = new DateTime(2024, 10, 27); //DateTime approach


List<DateTime> filteredAppointments = appointments.Where(appointment => DateOnly.FromDateTime(appointment) == targetDate).ToList(); //DateOnly comparison
//List<DateTime> filteredAppointments = appointments.Where(appointment => appointment.Date == targetDateOld.Date).ToList(); //DateTime comparison

foreach (DateTime appointment in filteredAppointments)
{
    Console.WriteLine(appointment);
}
// Output:
// 10/27/2024 10:00:00 AM
// 10/27/2024 02:00:00 PM
// 10/27/2024 11:00:00 AM

COMMENTS

DISQUS: 0