Convert time span value to format hhmm Am/Pm using C
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
TimeSpan in C# represents a duration (e.g., 14 hours and 30 minutes), not a time of day, so it has no concept of AM/PM. To format a TimeSpan as "hh:mm AM/PM", convert it to a DateTime first by adding the TimeSpan to DateTime.Today (which is midnight). Then use DateTime.ToString("hh:mm tt") where hh is the 12-hour clock and tt is the AM/PM designator.
Basic Conversion
DateTime.Today returns today's date at 00:00:00. Adding a TimeSpan of 14:30 gives a DateTime of today at 14:30, which formats as "02:30 PM".
Format Specifiers
| Specifier | Output | Description |
hh | 09 | 12-hour with leading zero |
h | 9 | 12-hour without leading zero |
HH | 09 | 24-hour with leading zero |
mm | 05 | Minutes with leading zero |
ss | 30 | Seconds with leading zero |
tt | AM/PM | AM or PM designator |
Helper Method
Working with Database Time Values
Databases often store time-of-day as TimeSpan (SQL Server's TIME type maps to TimeSpan in C#):
Extension Method
Culture-Specific Formatting
Parsing AM/PM String Back to TimeSpan
Common Pitfalls
- Using
HHinstead ofhhfor 12-hour format:HHis the 24-hour format specifier (00-23), whilehhis the 12-hour format (01-12). UsingHHwithttproduces "14:30 PM" which is incorrect — 14 is not a valid 12-hour value. - TimeSpan values greater than 24 hours: A
TimeSpanof 26:30:00 represents 1 day and 2.5 hours. Adding it toDateTime.Todaygives tomorrow at 02:30 AM, which formats as "02:30 AM". This may be misleading if the original intent was a duration, not a time of day. - Culture-dependent AM/PM designators: Some cultures do not use AM/PM (e.g., French, German). The
ttspecifier produces empty strings in these cultures. UseCultureInfo.InvariantCulturefor consistent AM/PM output regardless of the system locale. - Confusing midnight and noon:
TimeSpan(0, 0, 0)formats as "12:00 AM" (midnight) andTimeSpan(12, 0, 0)formats as "12:00 PM" (noon). This matches the standard 12-hour convention but can be confusing when reading rawTimeSpanvalues. - Using
TimeSpan.ToString()directly:TimeSpan.ToString("hh:mm tt")does not work as expected becauseTimeSpanformat specifiers are different fromDateTime. Thettspecifier is not valid forTimeSpan. Always convert toDateTimefirst.
Summary
- Convert
TimeSpantoDateTimeby adding it toDateTime.Today - Use
DateTime.ToString("hh:mm tt")for 12-hour AM/PM format hh= 12-hour padded,h= 12-hour unpadded,tt= AM/PM designator- Create an extension method
ToAmPmString()for reusable conversion - Use
CultureInfo.InvariantCulturefor consistent AM/PM output across locales - Parse AM/PM strings back to
TimeSpanviaDateTime.ParseExactthen.TimeOfDay
Related reading
- Convert.ChangeType fails on Nullable Types
- converting a .net FuncT to a .net ExpressionFuncT
- Converting a URI path to a relative file system path in .NET
- Converting BitmapImage to Bitmap and vice versa
- Converting country codes in .NET
- Converting Decimal to Double in C?
- Converting string to float in C
- Converting SVG to PNG using C

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.