the method below should accept 2400 as end datetime for the finishing time but the result is null;
-
5Check and see if your DateTime has the date of the next day after resetting to 0 instead of 24 – SimpleVar May 02 '12 at 09:15
-
Remove the catch and look for the exception. This will tell you which input string was in the wrong format. new DateTime(2012,5,2,24,0,0) will throw with an ArgumentOutOfRangeException. Your code is correct the input is wrong. – Alois Kraus May 02 '12 at 09:41
-
@YoryeNathan DateTime has a null date for the outDateTime. – Jason May 02 '12 at 09:47
-
@AloisKraus: System.ArgumentOutOfRangeException: Hour, Minute, and Second parameters describe an un-representable DateTime. – Jason May 02 '12 at 09:48
-
If hour is > 24 you need to parse it into a timespan and add it to inDateTime to get an outDateTime with the correct duration. Your current code does not take into account people working from 23:59:59 to 00:00:00 where the day changes. Even if you got this right then you will get errors when the day and the months changes... You need to parse the full date time and not use parts of it and fill it with different stuff. – Alois Kraus May 02 '12 at 09:55
-
"Your current code does not take into account people working from 23:59:59 to 00:00:00 where the day changes." Good point , can you help me with a hint please. – Jason May 02 '12 at 10:05
-
@AloisKraus . "You need to parse the full date time and not use parts of it and fill it with different stuff." -- I am Extracting my Time(1000,2300... format) values from textboxes in DataTable. Could you show me what I need to do to achieve my intended result? Thanks – Jason May 02 '12 at 12:11
-
You need to store in your table the full time. Year, Month, Day, Hour, Minute and if it does matter seconds. Then you can reconstruct the working duration simply by (outDateTime-inDateTime).Elapsed.TotalHours. Storing only half of the data and guessing the rest will lead to correctness problems. Filling up the working hours with DateTime.Now is not the best idea. – Alois Kraus May 02 '12 at 13:39
-
Thank you very much. I am about to do what you've just suggested and I hope to get it working. Thanks again – Jason May 02 '12 at 14:22
-
@AloisKraus Hello, I have manged to change textboxes to timepicker controls, however those don't accept 24:00 as a valid time -- It does acceprt 0000 as end time but I get - 2300 for (0000 to 2300). And just wonder what is the "Elapsed" from (outDateTime-inDateTime).Elapsed.TotalHours. Thanks – Jason May 03 '12 at 07:34
-
Perhaps you should not let the users enter invalid times. When you do subtract two DateTime instances you get a TimeSpan instance back which contains the Elapsed property. If the standard timepicker control does not let allow you to choose invalid times you should treat this as a warning sign of bad requirements. What would 25 mean? Or you need a second control to allow the user to add a duration in hours which is perhaps more meaningful. – Alois Kraus May 03 '12 at 15:51
3 Answers
From the MSDN spec for the DateTime.Hour
property.
The hour component, expressed as a value between 0 and 23.
Accordingly in the constructor for DateTime you are using, the hours integer must be a value between 0 and 23. 24 will throw an ArgumentOutOfRangeException
, which in your code will be caught by the empty catch block (generally empty catch block are a Bad Idea - see this SO answer).
If you want to handle the non-standard 2400 as a valid time, you'll need to add special handling for it.

- 1
- 1

- 2,637
- 24
- 30
-
thanks for mentioning the empty catch block. it helped me fixing an hidden bug. Thanks – Jason May 02 '12 at 14:24
ISO 8601 5.3.2 distinguishes between combined representations of DateTime (e.g. 2013-01-22 14:30:00) and uncombined time-only representations (e.g. 18:00). With uncombined time-only representations, to disambiguate the beginning of the day from the end of the day, the ISO spec states that the value 24:00 is a valid representation for the end of the 24-hour day. Microsoft does not follow this ISO specification on the .NET client or in SQL Server and you will need to use one of the workarounds suggested above. (Other databases such as PostgreSQL, DB2, and SQLite do adhere to the ISO spec.)

- 8,669
- 31
- 105
- 183
If you want to handle the non-standard 2400 as a valid time, you'll need to add special handling for it.
Hour % 2400

- 373
- 1
- 10