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

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

    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. The TimeSpan structure can also be used to represent the time of day, but only if the time is unrelated to a particular date. Otherwise, the DateTime or DateTimeOffset 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, the d component is days, hh is hours as measured on a 24-hour clock, mm is minutes, ss is seconds, and ff 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.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-28 07:40

    You can add the TimeSpan to a DateTime, for example:

    TimeSpan span = TimeSpan.FromHours(16);
    DateTime time = DateTime.Today + span;
    String result = time.ToString("hh:mm tt");
    

    Demo: http://ideone.com/veJ6tT

    04:00 PM
    

    Standard Date and Time Format Strings

    0 讨论(0)
  • 2020-11-28 07:42

    You will need to get a DateTime object from your TimeSpan and then you can format it easily.

    One possible solution is adding the timespan to any date with zero time value.

    var timespan = new TimeSpan(3, 0, 0);
    var output = new DateTime().Add(timespan).ToString("hh:mm tt");
    

    The output value will be "03:00 AM" (for english locale).

    0 讨论(0)
  • 2020-11-28 07:47

    You can try this:

       string timeexample= string.Format("{0:hh:mm:ss tt}", DateTime.Now);
    

    you can remove hh or mm or ss or tt according your need where hh is hour in 12 hr formate, mm is minutes,ss is seconds,and tt is AM/PM.

    0 讨论(0)
  • 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
    
    0 讨论(0)
提交回复
热议问题