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

前端 未结 11 1340
一生所求
一生所求 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条回答
  •  Happy的楠姐
    2020-11-28 07:39

    string displayValue="03:00 AM";
    

    This is a point in time , not a duration (TimeSpan).

    So something is wrong with your basic design or assumptions.

    If you do want to use it, you'll have to convert it to a DateTime (point in time) first. You can format a DateTime without the date part, that would be your desired string.

    TimeSpan t1 = ...;
    DateTime d1 = DateTime.Today + t1;               // any date will do
    string result = d1.ToString("hh:mm:ss tt");
    

    storeTime variable can have value like
    storeTime=16:00:00;

    No, it can have a value of 4 o'clock but the representation is binary, a TimeSpan cannot record the difference between 16:00 and 4 pm.

提交回复
热议问题