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.ToShortTimeString() Method in C#
The DateTime.ToShortTimeString() method in C# converts a DateTime object to its equivalent short time string representation. This method formats the time portion of the date using the current system culture's short time pattern, typically showing hours and minutes in 12-hour or 24-hour format.
Syntax
Following is the syntax −
public string ToShortTimeString();
Return Value
Returns a string that contains the short time string representation of the current DateTime object.
Using ToShortTimeString() with Current DateTime
This example demonstrates how to use ToShortTimeString() with the current system time and shows the culture-specific formatting pattern −
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.ToShortTimeString();
Console.WriteLine("Short time pattern = {0}", pattern.ShortTimePattern);
Console.WriteLine("Short time string representation = {0}", str);
}
}
The output of the above code is −
Date = 10/16/2019 8:59:23 AM Current culture = en-US Short time pattern = h:mm tt Short time string representation = 8:59 AM
Using ToShortTimeString() with Specific DateTime
This example shows how to format a specific DateTime object using ToShortTimeString() −
using System;
public class Demo {
public static void Main() {
DateTime d = new DateTime(2019, 11, 11, 7, 11, 25);
Console.WriteLine("Date = {0}", d);
string str = d.ToShortTimeString();
Console.WriteLine("Short time string representation = {0}", str);
}
}
The output of the above code is −
Date = 11/11/2019 7:11:25 AM Short time string representation = 7:11 AM
Using ToShortTimeString() with Different Time Values
This example demonstrates how the method formats different time values throughout the day −
using System;
public class Demo {
public static void Main() {
DateTime morning = new DateTime(2024, 1, 15, 8, 30, 0);
DateTime afternoon = new DateTime(2024, 1, 15, 14, 45, 30);
DateTime evening = new DateTime(2024, 1, 15, 23, 59, 59);
Console.WriteLine("Morning time: " + morning.ToShortTimeString());
Console.WriteLine("Afternoon time: " + afternoon.ToShortTimeString());
Console.WriteLine("Evening time: " + evening.ToShortTimeString());
}
}
The output of the above code is −
Morning time: 8:30 AM Afternoon time: 2:45 PM Evening time: 11:59 PM
Key Points
The method uses the current system culture's
ShortTimePatternfor formatting.Only the time portion is displayed − the date part is ignored.
The format typically excludes seconds, showing only hours and minutes.
In 12-hour cultures (like en-US), it includes AM/PM indicators.
Conclusion
The DateTime.ToShortTimeString() method provides a culture-aware way to format time values as readable strings. It automatically uses the system's regional settings to display time in the appropriate format, making it ideal for user-facing applications that need localized time representation.
