Convert Time Span Value to Format "Hh:Mm Am/Pm" Using C#

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

You can do this by adding your timespan to the date.

TimeSpan timespan = new TimeSpan(03,00,00);
DateTime time = DateTime.Today.Add(timespan);
string displayTime = time.ToString("hh:mm tt"); // It will give "03:00 AM"

How to convert timespan to pm or am time?

var time = DateTime.ParseExact("17:00", "HH:mm", null).ToString("hh:mm tt");

returns 05:00 PM

DateTime.ParseExact is returning DateTime


Include CultureInfo

var time = DateTime.ParseExact("17:00", "HH:mm", null).ToString("hh:mm tt", CultureInfo.GetCultureInfo("en-US"));

AM/PM to TimeSpan

The simplest approach would probably be to parse it as a DateTime using DateTime.ParseExact, and then use the TimeOfDay to exact the TimeSpan.

DateTime dateTime = DateTime.ParseExact(text,
"hh:mm tt", CultureInfo.InvariantCulture);
TimeSpan span = dateTime.TimeOfDay;

It's odd to see a leading 0 on a number of hours when you're also specifying an am/pm designator though. You might want "h" instead of "hh" in the format string, to allow "9:45 pm" instead of "09:45 pm".

(I'd also argue that it's a strange use of TimeSpan in the first place, but then the .NET date/time types are somewhat messed up in my view. I'd recommend using Noda Time, but I'm biased :)

Parsing AM/PM time string to TimeSpan

tt doesn't exist as one of the format specifiers for custom TimeSpan format strings. That makes sense in that TimeSpan is really meant to be a duration, not a time-of-day value - it's unfortunate that DateTime.TimeOfDay does return a TimeSpan.

It's probably simplest to parse the value as a DateTime and then get the time of day from that:

string text = "5:00 PM";
string[] formats = { "hhmm", "hmm", @"hh\:mm", @"h\:mm\:ss", @"h:mm", @"h:mm tt" };

var success = DateTime.TryParseExact(text, formats, CultureInfo.CurrentCulture,
DateTimeStyles.None, out var value);

Note that I've corrected the hh:mm tt to h:mm tt as your sample data isn't 0-padded. You quite possibly want HHmm, and HH:mm instead of hhmm and hh:mm as well, to accept values like "21:00".

Alternatively, use my Noda Time library that has a specific type for time-of-day (LocalTime) that can be parsed directly with a LocalTimePattern :)

Conversion of TimeSpan to a new variable on HHH: mm

Here is an example:

TimeSpan time = new TimeSpan(200,1,23);

string strTime = $"{((int)time.TotalHours).ToString("D3")}:{time.Minutes.ToString("D2")}";




Convert string.format and timespan to 12-hour format with AM/PM

It looks like you could change your code to return an IEnumerable<DateTime> pretty easily.

//Get your distinct time spans
var distinctTimeSpans = workingHours.Distinct();
//Build date objects from the parameter and time span objects
var dates = distinctTimeSpans.Select(ts => new DateTime(date.Value.Year, date.Value.Month, date.Value.Day, ts.Hours, ts.Minutes, ts.Seconds));

Then you can call ToString() on your DateTime object: .ToString("hh:mm tt")

How do I get the AM/PM value from a DateTime?

How about:

dateTime.ToString("tt", CultureInfo.InvariantCulture);

How do I get the AM/PM value from a just a Time not a date?

You'll need to convert it into a DateTime or write a custom formatter.

@string.Format("{0:hh:mm:ss tt}", new DateTime().Add(worker.BegginingTime))

Technically speaking TimeSpan doesn't store time of day, but rather duration.

Related Topics

Leave a reply