Forum Discussion

Harjot's avatar
Harjot
New Member
25 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 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".

2 Replies

  • Hassan_Ballan's avatar
    Hassan_Ballan
    Icon for Champion Level 3 rankChampion Level 3

    It would seem that you are looking at it as of adding 30 days rather than adding one month. What you are seeing is expected behavior described in https://support.smartbear.com/testcomplete/docs/reference/program-objects/aqdatetime/addmonths.html 

    Remarks

    If an invalid date is generated upon adding or subtracting a month, the last day of the resulting month is returned. For example:

    January 31st + 1 month = February 31st (not a valid day) = February 29th (leap year) or February 28th (non-leap year)

    May 31st - 1 month = April 31st (not a valid day) = April 30th

    💬 Found the answer helpful? Give it a Kudos by clicking Like!
    ✅ Got your issue resolved? Click Mark as Solution so others can find it quickly.

  • rraghvani's avatar
    rraghvani
    Icon for Champion Level 3 rankChampion Level 3

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