Convert time span value to format “hh:mm Am/Pm” using C#

前端 未结 11 1356
一生所求
一生所求 2020-11-28 07:22

I have a value stored in variable of type System.TimeSpan as follows.

System.TimeSpan storedTime = 03:00:00;

Can I re-store it

11条回答
  •  心在旅途
    2020-11-28 07:58

    Doing some piggybacking off existing answers here:

    public static string ToShortTimeSafe(this TimeSpan timeSpan)
    {
        return new DateTime().Add(timeSpan).ToShortTimeString();
    } 
    
    public static string ToShortTimeSafe(this TimeSpan? timeSpan)
    {
        return timeSpan == null ? string.Empty : timeSpan.Value.ToShortTimeSafe();
    }
    

提交回复
热议问题