Military (Midnight) DateTime subtraction
-
Is there an actual date associated with the value, or is it strictly time only? If it is time only, this explains why you're getting a negative value... A simple workaround may be to simply add 24 hours to your result if timeHere is negative, as you can hopefully assume nobody's time traveling and clocking out before they clock in. :)
LOL. It is just time only. So if timeOut.Hour == 0 then I use it to add 24 hours to it then do the subtraction right
-
How would I fix this problem I am having? I am reading datetime values from a database.
DateTime timeIn = DateTime.Parse(myReader["TimeIn"].ToString());
DateTime timeOut = DateTime.Parse(myReader["TimeOut"].ToString());TimeSpan timeHere = timeOut.Subtract(timeIn);
Now this code works perfectly except for dealing with midnight. If the person is here at 1:30PM and leaves at lets say 12:04AM, then how do I calulate that? It returns an hour of -13? The time is entered at 00:04 when put in the datebase (It doesn't let you enter 24:04)
-
LOL. It is just time only. So if timeOut.Hour == 0 then I use it to add 24 hours to it then do the subtraction right
Well, except then you'll get someone poor sod who works until 1am who blows that assumption up. :) I think you can do something like
if (timeHere < TimeSpan.Zero)
timeHere += TimeSpan.FromDays(1);I think this should work, as long as nobody is working longer than 24 hours shifts. ;) I don't have a compiler in front of me, so you may want to try a few values in that and make sure it works as expected, though. :)
-
Is there an actual date associated with the value, or is it strictly time only? If it is time only, this explains why you're getting a negative value... A simple workaround may be to simply add 24 hours to your result if timeHere is negative, as you can hopefully assume nobody's time traveling and clocking out before they clock in. :)
Ok I did timeOut = timeOut.AddHours(24); then completed it.. It works, thanks!
-
Ok I did timeOut = timeOut.AddHours(24); then completed it.. It works, thanks!
-
In military time, 00:04 is 12:04AM. There is no such thing as 24:04 in military time; military time starts at 0000 (midnight). If you are subtracting 1:30PM from 12:04AM for the same date, you should get a timespan result of -13:26.
Thanks, I knew there wasnt a 24 but it doesnt work if you subtract 00 from any other number to get the correct time.. So I added 24 to the time that held the midnight number to subtract the other time.
-
How would I fix this problem I am having? I am reading datetime values from a database.
DateTime timeIn = DateTime.Parse(myReader["TimeIn"].ToString());
DateTime timeOut = DateTime.Parse(myReader["TimeOut"].ToString());TimeSpan timeHere = timeOut.Subtract(timeIn);
Now this code works perfectly except for dealing with midnight. If the person is here at 1:30PM and leaves at lets say 12:04AM, then how do I calulate that? It returns an hour of -13? The time is entered at 00:04 when put in the datebase (It doesn't let you enter 24:04)
That would be why you should store the full date and time.
-
That would be why you should store the full date and time.
A true evangelist would add "according to ISO 8601" :laugh:
Luc Pattyn [Forum Guidelines] [My Articles]
- before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets
modified on Friday, June 10, 2011 11:32 PM
-
A true evangelist would add "according to ISO 8601" :laugh:
Luc Pattyn [Forum Guidelines] [My Articles]
- before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets
modified on Friday, June 10, 2011 11:32 PM
-
Well, not in this case, as if you would store a complete date and time you should store it as a datetime value, not a string... :)
Despite everything, the person most likely to be fooling you next is yourself.
True evangelists don't care about circumstances, they spread the gospel no matter what. ;P
Luc Pattyn [Forum Guidelines] [My Articles]
- before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets
modified on Friday, June 10, 2011 11:32 PM
-
A true evangelist would add "according to ISO 8601" :laugh:
Luc Pattyn [Forum Guidelines] [My Articles]
- before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets
modified on Friday, June 10, 2011 11:32 PM
Alas, no one listens. :sigh: And at least he was already using 24-hour time, so I didn't feel I had to invoke the gospel according to ISO 8601.
-
Ok I did timeOut = timeOut.AddHours(24); then completed it.. It works, thanks!