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
Long Time ("T") Format Specifier in C#
The Long Time ("T") format specifier in C# is a standard date and time format specifier that displays the time portion of a DateTime value in a long format. This format is culture-sensitive and displays the full time representation including hours, minutes, seconds, and AM/PM designator where applicable.
The "T" format specifier is defined by the DateTimeFormatInfo.LongTimePattern property and typically follows the pattern HH:mm:ss for 24-hour format or includes AM/PM for 12-hour format, depending on the culture.
Syntax
Following is the syntax for using the Long Time format specifier −
dateTime.ToString("T")
dateTime.ToString("T", CultureInfo.CreateSpecificCulture("culture-name"))
Using Long Time Format with Default Culture
Example
using System;
class Demo {
static void Main() {
DateTime date = new DateTime(2018, 9, 9, 8, 15, 30);
Console.WriteLine("Long Time Format: " + date.ToString("T"));
DateTime currentTime = DateTime.Now;
Console.WriteLine("Current Time (T): " + currentTime.ToString("T"));
}
}
The output of the above code is −
Long Time Format: 8:15:30 AM Current Time (T): 2:30:45 PM
Using Long Time Format with Specific Cultures
Example
using System;
using System.Globalization;
class Demo {
static void Main() {
DateTime date = new DateTime(2018, 9, 9, 14, 15, 30);
Console.WriteLine("US Culture: " + date.ToString("T", CultureInfo.CreateSpecificCulture("en-US")));
Console.WriteLine("UK Culture: " + date.ToString("T", CultureInfo.CreateSpecificCulture("en-GB")));
Console.WriteLine("French Culture: " + date.ToString("T", CultureInfo.CreateSpecificCulture("fr-FR")));
Console.WriteLine("German Culture: " + date.ToString("T", CultureInfo.CreateSpecificCulture("de-DE")));
}
}
The output of the above code is −
US Culture: 2:15:30 PM UK Culture: 14:15:30 French Culture: 14:15:30 German Culture: 14:15:30
Comparison with Other Time Format Specifiers
| Format Specifier | Name | Example Output (en-US) | Pattern |
|---|---|---|---|
| "t" | Short Time | 2:15 PM | h:mm tt |
| "T" | Long Time | 2:15:30 PM | h:mm:ss tt |
Using Long Time Format with Different DateTime Values
Example
using System;
using System.Globalization;
class Demo {
static void Main() {
DateTime morning = new DateTime(2023, 12, 25, 9, 30, 45);
DateTime afternoon = new DateTime(2023, 12, 25, 15, 45, 20);
DateTime midnight = new DateTime(2023, 12, 25, 0, 0, 0);
DateTime noon = new DateTime(2023, 12, 25, 12, 0, 0);
Console.WriteLine("Morning: " + morning.ToString("T"));
Console.WriteLine("Afternoon: " + afternoon.ToString("T"));
Console.WriteLine("Midnight: " + midnight.ToString("T"));
Console.WriteLine("Noon: " + noon.ToString("T"));
}
}
The output of the above code is −
Morning: 9:30:45 AM Afternoon: 3:45:20 PM Midnight: 12:00:00 AM Noon: 12:00:00 PM
Conclusion
The Long Time ("T") format specifier in C# provides a culture-sensitive way to display the complete time portion of a DateTime value, including hours, minutes, and seconds. It automatically adjusts the output format based on the current culture or a specified culture, making it ideal for internationalized applications that need to display time in a user-friendly format.
