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

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

    Parse timespan to DateTime and then use Format ("hh:mm:tt"). For example.

    TimeSpan ts = new TimeSpan(16, 00, 00);
    DateTime dtTemp = DateTime.ParseExact(ts.ToString(), "HH:mm:ss", CultureInfo.InvariantCulture);
    string str = dtTemp.ToString("hh:mm tt");
    

    str will be:

    str = "04:00 PM"
    

提交回复
热议问题