How to get only time from date-time C#

apeksha picture apeksha · Jun 22, 2009 · Viewed 340.4k times · Source

Suppose I have the value 6/22/2009 10:00:00 AM. How do I get only 10:00 Am from this date time.

Answer

Theofanis Pantelides picture Theofanis Pantelides · Jun 22, 2009

You have many options for this:

DateTime dt = DateTime.Parse("6/22/2009 07:00:00 AM");

dt.ToString("HH:mm"); // 07:00 // 24 hour clock // hour is always 2 digits
dt.ToString("hh:mm tt"); // 07:00 AM // 12 hour clock // hour is always 2 digits
dt.ToString("H:mm"); // 7:00 // 24 hour clock
dt.ToString("h:mm tt"); // 7:00 AM // 12 hour clock

Helpful Link: DateTime.ToString() Patterns