Domanda

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?

È stato utile?

Soluzione

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);
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top