C
C#2y ago
SWEETPONY

❔ [Error] The added or subtracted value results in an un-representable DateTime.

I have following function:
private static void BuildPeriodTable( WorktimeDataSet stds, DateTime begin, DateTime end )
{
for ( var day = begin; day < end; day = day.AddDays( 1 ) )
stds.PERIOD.AddPERIODRow( day.Date );
}
private static void BuildPeriodTable( WorktimeDataSet stds, DateTime begin, DateTime end )
{
for ( var day = begin; day < end; day = day.AddDays( 1 ) )
stds.PERIOD.AddPERIODRow( day.Date );
}
I don't know why but sometimes I get exception: "The added or subtracted value results in an un-representable DateTime" how can I deal with it?
7 Replies
TheRanger
TheRanger2y ago
You can only have a Date value between 1/1/0001 and 12/31/9999 11:59:59 PM you'd get this exception if you're trying to represent a date that is not between those dates
SWEETPONY
SWEETPONYOP2y ago
hmm no way in my case interesting that I don't get smth like ".. was not recognized as a valid DateTime"
TheRanger
TheRanger2y ago
what are the values of begin and end?
SWEETPONY
SWEETPONYOP2y ago
I will try to find them
TheRanger
TheRanger2y ago
no idea what AddPERIODRow does but other than that, the error is coming from day.AddDays(1) if it is then i believe end date is 31/12/9999 with some timespan in a for loop after the first loop, day = day.AddDays( 1 ) gets executed first before the loop executes day < end
Anton
Anton2y ago
If you want to account for that edge case, you need to do something like this.
if (end == begin)
return;
endInclusive = end.SubtractDays(1);
for (var day = begin; day <= endInclusive; day = day.AddDays(1))
//
if (end == begin)
return;
endInclusive = end.SubtractDays(1);
for (var day = begin; day <= endInclusive; day = day.AddDays(1))
//
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server