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
Edited:
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);
Console.WriteLine(value.TimeOfDay);
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")}";
Console.WriteLine(strTime);
Output:
200:01
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
Why C# Won't Allow Field Initializer with Non-Static Fields
Service Starting a Process Wont Show Gui C#
How to Set Read Permission on the Private Key File of X.509 Certificate from .Net
How to Get the Colour of a Pixel at X,Y Using C#
How Is the Boxing/Unboxing Behavior of Nullable<T> Possible
Why Do I Get "System.Data.Datarowview" Instead of Real Values in My Winforms Listbox
Giving Application Elevated Uac
Is That Possible to Send Httpwebrequest Using Tls1.2 on .Net 4.0 Framework
Parsing HTML to Get Content Using C#
Reading from Excel (Range into Multidimensional Array) C#
Simulating Keyboard with Sendinput API in Directinput Applications
Wpf Equivalent to Textrenderer
How to Make 'Always-On-Bottom'-Window
How to Export a Gridview.Datasource to a Datatable or Dataset
How to Put Conditional Required Attribute into Class Property to Work with Web API