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
DateTime.ToShortDateString() Method in C#
The DateTime.ToShortDateString() method in C# is used to convert the value of the current DateTime object to its equivalent short date string representation. This method formats the date according to the current culture's short date pattern, excluding the time portion.
Syntax
Following is the syntax −
public string ToShortDateString();
Return Value
This method returns a string that represents the short date value of the current DateTime object. The format depends on the current culture's DateTimeFormatInfo.ShortDatePattern property.
Using ToShortDateString() with Specific DateTime
Example
using System;
public class Demo {
public static void Main() {
DateTime d = new DateTime(2019, 08, 11, 9, 10, 45);
Console.WriteLine("Date = {0}", d);
string str = d.ToShortDateString();
Console.WriteLine("Short date string representation = {0}", str);
}
}
The output of the above code is −
Date = 8/11/2019 9:10:45 AM Short date string representation = 8/11/2019
Using ToShortDateString() with Current Culture
Example
using System;
using System.Globalization;
public class Demo {
public static void Main() {
DateTime d = DateTime.Now;
Console.WriteLine("Date = {0}", d);
Console.WriteLine("Current culture = " + CultureInfo.CurrentCulture.Name);
var pattern = CultureInfo.CurrentCulture.DateTimeFormat;
string str = d.ToShortDateString();
Console.WriteLine("Short date pattern = {0}", pattern.ShortDatePattern);
Console.WriteLine("Short date string representation = {0}", str);
}
}
The output of the above code is −
Date = 10/16/2019 8:56:40 AM Current culture = en-US Short date pattern = M/d/yyyy Short date string representation = 10/16/2019
Comparing Different Date Formats
Example
using System;
using System.Globalization;
public class Demo {
public static void Main() {
DateTime currentDate = new DateTime(2023, 12, 25);
// Default culture (en-US)
Console.WriteLine("Default culture:");
Console.WriteLine("Full DateTime: " + currentDate.ToString());
Console.WriteLine("Short Date: " + currentDate.ToShortDateString());
Console.WriteLine("Long Date: " + currentDate.ToLongDateString());
// Change culture to demonstrate different formats
CultureInfo.CurrentCulture = new CultureInfo("en-GB");
Console.WriteLine("\nBritish culture (en-GB):");
Console.WriteLine("Short Date: " + currentDate.ToShortDateString());
}
}
The output of the above code is −
Default culture: Full DateTime: 12/25/2023 12:00:00 AM Short Date: 12/25/2023 Long Date: Monday, December 25, 2023 British culture (en-GB): Short Date: 25/12/2023
Key Features
| Feature | Description |
|---|---|
| Culture Dependent | Format varies based on current culture settings |
| Date Only | Returns only the date portion, excluding time |
| Pattern Based | Uses culture's ShortDatePattern property |
| Immutable | Does not modify the original DateTime object |
Conclusion
The DateTime.ToShortDateString() method provides a culture-sensitive way to format dates as short strings. It automatically adapts to the current culture's date formatting conventions, making it ideal for user interface display where localized date formats are important.
