Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to convert 24-hour format TimeSpan to 12-hour format TimeSpan?

Tags:

c#

I have TimeSpan data represented as 24-hour format, such as 14:00:00, I wanna convert it to 12-hour format, 2:00 PM, I googled and found something related in stackoverflow and msdn, but didn't solve this problem, can anyone help me? Thanks in advance.

Update Seems that it's possible to convert 24-hour format TimeSpan to String, but impossible to convert the string to 12-hour format TimeSpan :(

But I still got SO MANY good answers, thanks!

like image 477
user1108069 Avatar asked Apr 12 '12 12:04

user1108069


People also ask

How do you convert a 24 hour system to a 12 hour system?

Converting from 12 hour to 24 hour clock Starting from the first hour of the day (12:00 AM or midnight to 12:59 AM), subtract 12 hours: 12:00 AM = 0:00. 12:15 AM = 0:15.

How do you convert TimeSpan to hours?

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. The value of the Hours property is the hours component, hh.


2 Answers

(Summing up my scattered comments in a single answer.)

First you need to understand that TimeSpan represents a time interval. This time interval is internally represented as a count of ticks an not the string 14:00:00 nor the string 2:00 PM. Only when you convert the TimeSpan to a string does it make sense to talk about the two different string representations. Switching from one representation to another does not alter or convert the tick count stored in the TimeSpan.

Writing time as 2:00 PM instead of 14:00:00 is about date/time formatting and culture. This is all handled by the DateTime class.

However, even though TimeSpan represents a time interval it is quite suitable for representing the time of day (DateTime.TimeOfDay returns a TimeSpan). So it is not unreasonable to use it for that purpose.

To perform the formatting described you need to either rely on the formatting logic of DateTime or simply create your own formatting code.

  • Using DateTime:

    var dateTime = new DateTime(timeSpan.Ticks); // Date part is 01-01-0001
    var formattedTime = dateTime.ToString("h:mm tt", CultureInfo.InvariantCulture);
    

    The format specifiers using in ToString are documented on the Custom Date and Time Format Strings page on MSDN. It is important to specify a CultureInfo that uses the desired AM/PM designator. Otherwise the tt format specifier may be replaced by the empty string.

  • Using custom formatting:

    var hours = timeSpan.Hours;
    var minutes = timeSpan.Minutes;
    var amPmDesignator = "AM";
    if (hours == 0)
      hours = 12;
    else if (hours == 12)
      amPmDesignator = "PM";
    else if (hours > 12) {
      hours -= 12;
      amPmDesignator = "PM";
    }
    var formattedTime =
      String.Format("{0}:{1:00} {2}", hours, minutes, amPmDesignator);
    

    Admittedly this solution is quite a bit more complex than the first method.

like image 77
Martin Liversage Avatar answered Oct 11 '22 05:10

Martin Liversage


TimeSpan represents a time interval not a time of day. The DateTime structure is more likely what you're looking for.

like image 39
M.Babcock Avatar answered Oct 11 '22 06:10

M.Babcock