Reputation: 46222
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
Reputation: 7797
With format strings.
DateTime Time = DateTime.Now;
Time.ToString("h tt");
Upvotes: 4
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
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