leora
leora

Reputation: 196459

how can you format just the time of a DateTime object in C#

I have a DateTime object. How can I print just the time and in the following format:

 5:50 PM

or

 3:30 AM

Upvotes: 1

Views: 82

Answers (3)

ptfaulkner
ptfaulkner

Reputation: 712

Try doing .ToShortTimeString() on your date object. For example:

DateTime.Now.ToShortTimeString();

Upvotes: 2

heisenberg
heisenberg

Reputation: 9759

yourDateTimeVariable.ToString("h:mm tt");

Upvotes: 3

ojlovecd
ojlovecd

Reputation: 4892

    DateTime dt = DateTime.Now;
    Console.WriteLine(dt.ToString("h:mm tt", new System.Globalization.CultureInfo("en-us")));

try this

Upvotes: 2

Related Questions