<asp:Label ID="timeLabel" runat="server" Text='<%# Eval("time") %>' />
In this statement, "time" is a time datatype in my database. It displays as 16:00:00. I am trying to get this to display as 4:00 PM. I think this is pretty straight forward if I were working with a datetime datatype, but how can I convert this to datetime (to use the {HH.mm tt} formatting) or otherwise display with as AM/PM time.
Since T-SQL
timeis mapped withTimeSpanin CLR side, when you get this value from your database, it will beTimeSpan, not aDateTime.And you can't represent a
TimeSpanwithAMorPMdesignators. These are only forDateTimerepresentations. There is no such a thing aTimeSpanlike; 4 hour time interval afternoon. That doesn't make sense, right?Closest thing might be, getting this value from your database and add it to your
DateTime.Todayvalue withAdd(TimeSpan)method overload. Then you can get it's string representation with.ToString("h:mm tt")method with a culture that doesn't haveAMDesignatorandPMDesignatoras an empty string.Step by step;
timeas aTimeSpanfrom your database.DateTime dt = DateTime.Today.Add(time)method.dt.ToString("h:mm tt", CultureInfo.InvariantCulture)