You cannot add AM / PM to a TimeSpan
. You'll anyway have to associate the TimaSpan
value with DateTime
if you want to display the time in 12-hour clock format.
TimeSpan
is not intended to use with a 12-hour clock format, because we are talking about a time interval here.
As it says in the documentation;
A
TimeSpan
object represents a time interval (duration of time or elapsed time) that is measured as a positive or negative number of days, hours, minutes, seconds, and fractions of a second. TheTimeSpan
structure can also be used to represent the time of day, but only if the time is unrelated to a particular date. Otherwise, theDateTime
orDateTimeOffset
structure should be used instead.
Also Microsoft Docs describes as follows;
A
TimeSpan
value can be represented as[-]d.hh:mm:ss.ff
, where the optional minus sign indicates a negative time interval, thed
component is days,hh
is hours as measured on a 24-hour clock,mm
is minutes,ss
is seconds, andff
is fractions of a second.
So in this case, you can display using AM/PM as follows.
TimeSpan storedTime = new TimeSpan(03,00,00);
string displayValue = new DateTime().Add(storedTime).ToString("hh:mm tt");
Side note :
Also should note that the TimeOfDay property of DateTime
is a TimeSpan
, where it represents
a time interval that represents the fraction of the day that has elapsed since midnight.