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

前端 未结 11 1369
一生所求
一生所求 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:51

    At first, you need to convert time span to DateTime structure:

    var dt = new DateTime(2000, 12, 1, timeSpan.Hours, timeSpan.Minutes, timeSpan.Seconds)
    

    Then you need to convert the value to string with Short Time format

    var result = dt.ToString("t"); // Convert to string using Short Time format
    

提交回复
热议问题