How to truncate seconds from timespan in asp.net mvc?
In a form of my asp.net mvc app I have a dropdown displaying Start time as well as End time, like 8:00:00 through 22:00:00. In the database these both fields' data type is Timespan. Is it possible to show the time in standard format (am/pm), without displaying seconds, like 8:00 AM or just 8:00? I'm using .Net 3.5 and I don't know how and where in the app. to c开发者_C百科onvert the timespan to datetime. Thank you!
Add the timespan to DateTime.Today, then use a format string for the output you want.
var startTime = DateTime.Today + Start;
Console.WriteLine( startTime.ToString( "HH:mm tt" ) );
As of .NET 4, TimeSpan.ToString()
has an overload that accepts a format string:
Console.WriteLine(Start.ToString("h:mm"));
精彩评论