Processing time over 24 hours in C #

Suppose a timestamp (just the time or date and time) where the time can move to the next day:

00:00:00 & midnight

01:00:00 <- 1 AM

23:00:00 <- 11 PM

24:00:00 - midnight, day + 1

25:00:00 - 1:00, day + 1

What is the easy way to parse it in C # DateTime, which will do the transfer the next day? In other words, โ€œ01:00:00โ€ will become โ€œ0001-01-01 01:00:00โ€ and โ€œ25:00:00โ€ will become โ€œ0001-01-02 01:00:00โ€.

EDIT:

I should mention that this fails (e.g. FormatException):

DateTime.ParseExact("0001-01-01 25:00:00", "yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture); 
+7
source share
5 answers

Since you are trying to represent a time period from an arbitrary point, and not as a specific date, maybe you would be better off using the System.TimeSpan class? This allows you to set values โ€‹โ€‹in the constructor for more than 24 hours and can be used with DateTime objects as follows:

 System.TimeSpan timestamp = new System.TimeSpan(25, 0, 0); System.DateTime parsedDateTime = new DateTime(0, 0, 0); parsedDateTime = parsedDateTime.Add(timestamp); Console.WriteLine(parsedDateTime.ToString("yyyy-MM-dd HH:mm:ss")); //Output as "0001-01-02 01:00:00" 

NOTE. Code not verified.

EDIT:. In terms of parsing strings, I can't think of any basic .NET objects that parse strings with values โ€‹โ€‹greater than 23 for an hour (since 25 is an invalid hour of the day), but assuming the format is consistent, you can create a very simple a line parsing procedure (or even a regular expression) to read the values โ€‹โ€‹individually and manually load the constructor.

+8
source

If you have an existing DateTime value that you can add, you can always use TimeSpan:

 string dt = "25:00:00"; int hours = int.Parse(dt.Split(':')[0]); TimeSpan ts = TimeSpan.FromHours(hours); 

TimeSpan.Parse() does not work directly in this case, because it complains (rightly!) Of about 25 in hour designations.

+2
source

Indication of an invalid date. Thus, you can not only disassemble it, but also can not save it!

What about a beautiful TimeSpan object? (It also has a Parse() method.)

Alternatively, use the sscanf() -type function, such as the http://www.blackbeltcoder.com/Articles/strings/a-sscanf-replacement-for-net function, to extract each number individually. (Better if you do not control the read string format.)

+1
source

If no one points to a ready-made answer, here is a neat ActionScript class that I wrote to parse temporary inputs (human input) ...

https://github.com/appcove/AppStruct/blob/master/Flex/AppStruct/src/AppStruct/TimeInput.as

It would be very simple to port this to C #, and you could tweak the 24 hour logic to get #days, #hours, #minutes.

Good luck

+1
source

If you want to code the code ... this should be the starting point:

  string dateString = "0001-01-01 25:00:00"; string[] parts = dateString.Split(' '); //now have '0001-01-01' and '25:00:00' string datePart = parts[0]; // '0001-01-01' string[] timeParts = parts[1].Split(':'); //now have '25', '00', and '00 DateTime initialDate = DateTime.ParseExact(datePart, "yyyy-MM-dd", CultureInfo.InvariantCulture);//use the date as a starting point //use the add methods to get your desired datetime int hours = int.Parse(timeParts[0]); int minutes = int.Parse(timeParts[1]); int seconds = int.Parse(timeParts[2]); DateTime resultDate = initialDate.AddHours(hours) .AddMinutes(minutes) .AddSeconds(seconds); 

Of course, he makes assumptions that the input is formatted correctly and is collapsible, etc. In addition, you can definitely use time intervals instead of separate adding methods for an hour, minute, second, like some other answers ..

+1
source

All Articles