Is there a safe check I can do before calling DateTime.AddMonths (int months)?

If I call AddMonths on a DateTime object using too large an int, I get an ArgumentException written on my face with a polite message saying:

"An added or subtracted value results in an unimaginable DateTime. Parameter name: months"

What check should be done in the months argument before calling this method?

+3
source share
2 answers

From MSDN :

ArgumentOutOfRangeException

The result of a DateTime is less than MinValue or greater than MaxValue .

-or -

-120 000 120 000.

, , 120 000.

, , , .

+1

AddMonths try, .

+1

Source: https://habr.com/ru/post/1731004/


All Articles