Incorrect Date Calculation When Adding One Month with AddMonths Function
I'm experiencing unexpected behavior when using the AddMonths function in TestComplete.
When I input the date 30/01/2028 and add 1 month using AddMonths(1), the result is 29/02/2028 instead of the expected 01/03/2028.
The same issue occurs with the date 29/01/2028, which also returns 29/02/2028 instead of 29/02/2028 or 01/03/2028, depending on expectations.
Additionally, when using the date 31/08/2028, the result after adding 1 month is 30/09/2028, whereas I would expect 01/10/2028 in alignment with the day overflow logic.
It seems the AddMonths function does not correctly handle month transitions for dates near the end of the month, particularly in leap years or months with fewer days.
Please investigate and clarify whether this is intended behavior or a bug in the date calculation logic.
January 30, 2028 + 1 month would normally be February 30, 2028. However, February 2028 is a leap year (2028 is divisible by 4), so February has 29 days. Since February 30 doesn’t exist, the date rolls back to February 29, 2028. So, the final date is 29th February 2028.
function test() { var mydate = aqDateTime.SetDateElements(2028, 1, 30); var date = aqConvert.DateTimeToFormatStr(mydate, "%d/%m/%Y"); Log.Message(date); var newdate = aqDateTime.AddMonths(date, 1); Log.Message(newdate); }
This is expected behaviour for most date/time libraries including those based on .NET, Java, Python, and TestComplete as well.
If you add days instead, then you will get March 1, 2028
function test() { var mydate = aqDateTime.SetDateElements(2028, 1, 30); var date = aqConvert.DateTimeToFormatStr(mydate, "%d/%m/%Y"); Log.Message(date); var newdate = aqDateTime.AddDays(date, 31); Log.Message(newdate); }
This is referred to as "function as designed".