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.ToLongTimeString() Method in C#
The DateTime.ToLongTimeString() method in C# is used to convert the value of the current DateTime object to its equivalent long time string representation. This method formats the time portion of a DateTime object according to the current culture's long time format pattern.
Syntax
Following is the syntax −
public string ToLongTimeString();
Return Value
Returns a string that contains the long time string representation of the current DateTime object, formatted according to the current culture's long time pattern.
Using ToLongTimeString() with Current DateTime
The method uses the current system culture to determine the format pattern for the long time string −
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.ToLongTimeString();
Console.WriteLine("Long time pattern = {0}", pattern.LongTimePattern);
Console.WriteLine("Long time string representation = {0}", str);
}
}
The output of the above code is −
Date = 10/16/2019 8:41:03 AM Current culture = en-US Long time pattern = h:mm:ss tt Long time string representation = 8:41:03 AM
Using ToLongTimeString() with Specific DateTime
You can also use this method with a specific DateTime object created using the DateTime constructor −
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.ToLongTimeString();
Console.WriteLine("Long time string representation = {0}", str);
}
}
The output of the above code is −
Date = 11/11/2019 7:11:25 AM Long time string representation = 7:11:25 AM
Comparison with Other Time String Methods
| Method | Format Pattern (en-US) | Example Output |
|---|---|---|
| ToLongTimeString() | h:mm:ss tt | 8:41:03 AM |
| ToShortTimeString() | h:mm tt | 8:41 AM |
| ToString("T") | HH:mm:ss | 08:41:03 |
Culture-Specific Formatting
using System;
using System.Globalization;
public class Demo {
public static void Main() {
DateTime dt = new DateTime(2023, 12, 25, 14, 30, 45);
// Default culture
Console.WriteLine("Default: " + dt.ToLongTimeString());
// French culture
CultureInfo.CurrentCulture = new CultureInfo("fr-FR");
Console.WriteLine("French: " + dt.ToLongTimeString());
// German culture
CultureInfo.CurrentCulture = new CultureInfo("de-DE");
Console.WriteLine("German: " + dt.ToLongTimeString());
}
}
The output of the above code is −
Default: 2:30:45 PM French: 14:30:45 German: 14:30:45
Conclusion
The ToLongTimeString() method provides a convenient way to format the time portion of a DateTime object according to the current culture's long time pattern. It automatically handles culture-specific formatting, making it ideal for displaying time information to users in their preferred format.
