In this article, we will learn how to get the only time part from DateTime in C#. we will use the DateTime
object to initialize the date with time. And use the ToString()
method to format the DateTime
to get the time. In this program, we will use time format specifiers to get the time only from the Datetime
object.
Here is the source code of the program to get only time part from the DateTime in C#
using System;
namespace Tutorialsrack
{
class Program
{
/* How to Get Only Time Part From Datetime in C# */
static void Main(string[] args)
{
//initialize a datetime variable
DateTime date = new DateTime(2019, 10, 12, 15, 50, 00);
//Using TimeSpan
TimeSpan TodayTime = date.TimeOfDay;
Console.WriteLine("Time: {0}", TodayTime);
//Output ==> Time: 15:50:00
Console.WriteLine("Time: {0}", date.ToString("t"));
//Output ==> Time: 15:50
Console.WriteLine("Time: {0}", date.ToString("T"));
//Output ==> Time: 15:50:00
Console.WriteLine("Time: {0}", date.ToLongTimeString());
//Output ==> Time: 15:50:00
Console.WriteLine("Time: {0}", date.ToShortTimeString());
//Output ==> Time: 15:50
Console.WriteLine("Time: {0}", date.GetDateTimeFormats('T')[0]);
//Output ==> Time: 15:50:00
Console.WriteLine("Format: HH:mm tt and Time is: {0}", date.ToString("HH:mm tt"));
//Output ==> Format: HH:mm tt and Time is: 15:50 PM
Console.WriteLine("Format: HH:mm:ss tt and Time is: {0}", date.ToString("HH:mm:ss tt"));
//Output ==> Format: HH:mm:ss tt and Time is: 15:50:00 PM
Console.WriteLine("Format: HH:mm:ss:ffff tt and Time is: {0}", date.ToString("HH:mm:ss:ffff tt"));
//Output ==> Format: HH:mm:ss:ffff tt and Time is: 15:50:00:0000 PM
Console.WriteLine("Format: HH:mm:ss and Time is: {0}", date.ToString("HH:mm:ss"));
//Output ==> Format: HH:mm:ss and Time is: 15:50:00
Console.WriteLine("Format: HH:mm and Time is: {0}", date.ToString("HH:mm"));
//Output ==> Format: HH:mm and Time is: 15:50
Console.WriteLine("Format: hh:mm tt and Time is: {0}", date.ToString("hh:mm tt"));
//Output ==> Format: hh:mm tt and Time is: 03:50 PM
Console.WriteLine("Format: hh:mm:ss tt and Time is: {0}", date.ToString("hh:mm:ss tt"));
//Output ==> Format: hh:mm:ss tt and Time is: 03:50:00 PM
Console.WriteLine("Format: hh:mm:ss:fffffff tt and Time is: {0}", date.ToString("hh:mm:ss:fffffff tt"));
//Output ==> Format: hh:mm:ss:fffffff tt and Time is: 03:50:00:0000000 PM
Console.WriteLine("Format: hh:mm:ss and Time is: {0}", date.ToString("hh:mm:ss"));
//Output ==> Format: hh:mm:ss and Time is: 03:50:00
Console.WriteLine("Format: hh:mm and Time is: {0}", date.ToString("hh:mm"));
//Output ==> Format: hh:mm and Time is: 03:50
Console.ReadKey();
}
}
}
Time: 15:50:00
Time: 15:50
Time: 15:50:00
Time: 15:50:00
Time: 15:50
Time: 15:50:00
Format: HH:mm tt and Time is: 15:50 PM
Format: HH:mm:ss tt and Time is: 15:50:00 PM
Format: HH:mm:ss:ffff tt and Time is: 15:50:00:0000 PM
Format: HH:mm:ss and Time is: 15:50:00
Format: HH:mm and Time is: 15:50
Format: hh:mm tt and Time is: 03:50 PM
Format: hh:mm:ss tt and Time is: 03:50:00 PM
Format: hh:mm:ss:fffffff tt and Time is: 03:50:00:0000000 PM
Format: hh:mm:ss and Time is: 03:50:00
Format: hh:mm and Time is: 03:50
I hope this article will help you to understand how to get the only time part from DateTime in C#.
Share your valuable feedback, please post your comment at the bottom of this article. Thank you!
Comments