Question

I have a string that looks like "05:00:00". I'm using the following to convert it to a datetime in C#.

var time = DateTime.ParseExact(row.l_file_time, "HH:mm:ss", System.Globalization.CultureInfo.GetCultureInfo("en-US" )); var x = time.ToString("tt");

c# thinks 05:00 is on the morning and it should be PM. Is there a way to conversion take account the times are based in the work hours?

Was it helpful?

Solution

No, DateTime uses 24-hour time, so parsing "5:00" will always return the equivalent of 5 AM.

"Work hours" are not constant, even within a culture (maybe I work at night!) so any kind of logic like this needs to be written by you, and is not provided in the framework.

This logic could easily look like:

if (time.Hour < 9) // less than 9AM, it must be PM!
  time.AddHours(12);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top