It all depends on
dateFrom
and
dateTo
. Split will return empty array if they are empty strings and only one element in the array if there is no "/" delimiters. If first case, index 0 will be invalid, in second, more likely case, index 1 will be invalid. Isn't it obvious? Anyway, in any doubt, use the debugger, always.
But your whole approach is wrong. With .NET, you should not parse time values in such a home-baked way. Instead, use these methods:
https://msdn.microsoft.com/en-us/library/system.datetime.parse%28v=vs.110%29.aspx[
^],
https://msdn.microsoft.com/en-us/library/system.datetime.parseexact%28v=vs.110%29.aspx[
^],
https://msdn.microsoft.com/en-us/library/system.datetime.tryparse%28v=vs.110%29.aspx[
^],
https://msdn.microsoft.com/en-us/library/system.datetime.tryparseexact%28v=vs.110%29.aspx[
^].
As you can see, altogether, where are many similar methods. You can control the format of expected string by culture or exact format string.
But let me make another step forward: I'm not sure that you need to parse any strings at all. It could be simply the result of the attempt of working with strings representing data instead data itself, a very "popular" sorrowful trend. Normally, you need a string only if you output some time information in screen. Usually, it's possible to work only with
System.DateTime
(and sometime
System.TimeSpan
type, without any parsing. For example, input of time values can be done through some kind of already available date/time input control or ask current time from the OS.
—SA