Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to get only Date portion from DateTime object in C#?
There are several ways to extract only the date portion from a DateTime object in C#. Each method serves different purposes depending on whether you need a string representation or want to preserve the DateTime type.
Methods Overview
| Method | Return Type | Description |
|---|---|---|
ToShortDateString() |
String | Returns culture-specific short date format |
ToLongDateString() |
String | Returns culture-specific long date format |
ToString(format) |
String | Returns custom formatted date string |
DateTime.Date |
DateTime | Returns DateTime with time set to 00:00:00 |
Using ToShortDateString() and ToLongDateString()
These methods provide culture-specific date string representations −
using System;
namespace DemoApplication {
public class Program {
public static void Main() {
var dateTime = new DateTime(2024, 8, 15, 14, 30, 45);
Console.WriteLine($"DateTime Value: {dateTime}");
var shortDateValue = dateTime.ToShortDateString();
Console.WriteLine($"Short Date Value: {shortDateValue}");
var longDateValue = dateTime.ToLongDateString();
Console.WriteLine($"Long Date Value: {longDateValue}");
}
}
}
The output of the above code is −
DateTime Value: 8/15/2024 2:30:45 PM Short Date Value: 8/15/2024 Long Date Value: Thursday, August 15, 2024
Using DateTime.Date Property
The Date property returns a DateTime object with the time component set to midnight (00:00:00). This preserves the DateTime type rather than converting to a string −
using System;
namespace DemoApplication {
public class Program {
public static void Main() {
var dateTime = new DateTime(2024, 8, 15, 14, 30, 45);
Console.WriteLine($"DateTime Value: {dateTime}");
var dateValue = dateTime.Date;
Console.WriteLine($"Date Value: {dateValue}");
Console.WriteLine($"Type: {dateValue.GetType().Name}");
}
}
}
The output of the above code is −
DateTime Value: 8/15/2024 2:30:45 PM Date Value: 8/15/2024 12:00:00 AM Type: DateTime
Using ToString() with Custom Formats
The ToString() method with format specifiers provides the most control over date formatting −
using System;
namespace DemoApplication {
public class Program {
public static void Main() {
var dateTime = new DateTime(2024, 8, 15, 14, 30, 45);
Console.WriteLine($"DateTime Value: {dateTime}");
var dateValue1 = dateTime.ToString("MM/dd/yyyy");
Console.WriteLine($"US Format: {dateValue1}");
var dateValue2 = dateTime.ToString("dd/MM/yyyy");
Console.WriteLine($"European Format: {dateValue2}");
var dateValue3 = dateTime.ToString("yyyy-MM-dd");
Console.WriteLine($"ISO Format: {dateValue3}");
var dateValue4 = dateTime.ToString("d/M/yy");
Console.WriteLine($"Short Format: {dateValue4}");
}
}
}
The output of the above code is −
DateTime Value: 8/15/2024 2:30:45 PM US Format: 08/15/2024 European Format: 15/08/2024 ISO Format: 2024-08-15 Short Format: 15/8/24
Comparison of Methods
Here's a practical comparison showing when to use each method −
using System;
namespace DemoApplication {
public class Program {
public static void Main() {
var dateTime = new DateTime(2024, 8, 15, 14, 30, 45);
// For display purposes
Console.WriteLine("For Display:");
Console.WriteLine($"Short: {dateTime.ToShortDateString()}");
Console.WriteLine($"Long: {dateTime.ToLongDateString()}");
// For calculations (preserves DateTime type)
Console.WriteLine("\nFor Calculations:");
var dateOnly = dateTime.Date;
var tomorrow = dateOnly.AddDays(1);
Console.WriteLine($"Today: {dateOnly:yyyy-MM-dd}");
Console.WriteLine($"Tomorrow: {tomorrow:yyyy-MM-dd}");
// For specific formats
Console.WriteLine("\nCustom Formats:");
Console.WriteLine($"Database: {dateTime:yyyy-MM-dd}");
Console.WriteLine($"File name: {dateTime:yyyyMMdd}");
}
}
}
The output of the above code is −
For Display: Short: 8/15/2024 Long: Thursday, August 15, 2024 For Calculations: Today: 2024-08-15 Tomorrow: 2024-08-16 Custom Formats: Database: 2024-08-15 File name: 20240815
Conclusion
Use DateTime.Date when you need to preserve the DateTime type for further calculations. Use ToString() with format specifiers for custom date string formats, and use ToShortDateString() or ToLongDateString() for culture-specific display formats.
