Forum Discussion

Harjot's avatar
Harjot
New Member
28 days ago
Solved

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 e...
  • rraghvani's avatar
    28 days ago

    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".