Get current time without date in C#
This post will discuss how to get the current time without a date in C#.
The DateTime.Now property returns a DateTime object whose value is the current date and time. To get the time component of DateTime.Now, use any of the following methods:
1. Using DateTime.ToString() method
The DateTime.ToString() method can be used to get the string representation of the current DateTime object in a specified format.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
using System; public class Example { public static void Main() { string time = DateTime.Now.ToString("h:mm:ss tt"); Console.WriteLine("The current time is {0}", time); } } /* Sample Output: The current time is 7.56.24 PM */ |
Alternatively, we can use the short time t format specifier, which displays the value of the current DateTime object value using a short time pattern.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
using System; public class Example { public static void Main() { string time = DateTime.Now.ToString("t"); Console.WriteLine("The current time is {0}", time); } } /* Sample Output: The current time is 07.57 PM */ |
We can also use a long time T format specifier that displays the value of the current DateTime object value using a long time pattern.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
using System; public class Example { public static void Main() { string time = DateTime.Now.ToString("T"); Console.WriteLine("The current time is {0}", time); } } /* Sample Output: The current time is 7.58.29 PM */ |
2. Using DateTime.ToShortTimeString() method
The DateTime.ToShortTimeString() method can be used to convert the value of the current DateTime object to its equivalent short time string representation.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
using System; public class Example { public static void Main() { string time = DateTime.Now.ToShortTimeString(); Console.WriteLine("The current time is {0}", time); } } /* Sample Output: The current time is 07.59 PM */ |
3. Using DateTime.ToLongTimeString() method
The DateTime.ToLongTimeString() method can be used to convert the value of the current DateTime object to its equivalent long time string representation.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
using System; public class Example { public static void Main() { string time = DateTime.Now.ToLongTimeString(); Console.WriteLine("The current time is {0}", time); } } /* Sample Output: The current time is 7.59.33 PM */ |
4. Using DateTime.GetDateTimeFormats() method
The DateTime.GetDateTimeFormats() method returns an array of all the string representations of the current DateTime object supported by the specified format specifier. The idea is to pass the T format specifier to the method and print the first value in the returned array.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
using System; public class Example { public static void Main() { string time = DateTime.Now.GetDateTimeFormats('T')[0]; Console.WriteLine("The current time is {0}", time); } } /* Sample Output: The current time is 8.00.26 PM */ |
That’s all about getting the current time without date in C#.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)