Nate Pet
Nate Pet

Reputation: 46222

c# DateTime need to show only Time

There is a field defined in the SQL Server DB as DateTime. I need to show only the time - 5 PM . Is it possible through C# to have it show only the Time so that it can go in the DB properly. If so, how do you display the time in C# as 5 PM?

Upvotes: 0

Views: 20168

Answers (3)

Doozer Blake
Doozer Blake

Reputation: 7797

With format strings.

DateTime Time = DateTime.Now;
Time.ToString("h tt");

Upvotes: 4

Ivan Crojach Karačić
Ivan Crojach Karačić

Reputation: 1911

This will do the job

DateTime date= new DateTime(2008, 4, 1, 18, 53, 0);
Console.WriteLine(date.ToString("h tt"));  // Displays 6 PM

Upvotes: 4

John Kraft
John Kraft

Reputation: 6840

DateTime.Now.ToShortTimeString() is probably the closest you're gonna do without a custom format string. The output looks like

8:54 AM

So, it doesn't matter how you store it in the database. ToShortTimeString() will only display the time.

Upvotes: 7

Related Questions