Get Only Time out of DateTime Variable in C#

I was working with C# web-service today and had a requirement to get the time in HH:mm format and save it in a String variable. Here how you do it in C#

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
Did you enjoy this article?
Signup today and receive free updates straight in your inbox. We will never share or sell your email address.

4 Comments

  1. Just what i wanted

    Reply
  2. cool :) Glad it helped u.

    Reply
  3. Thanks Vikram :)

    Reply

Leave a Reply