Forum Discussion

tarleaa's avatar
tarleaa
Contributor
13 years ago

aqDateTime issue

Hello, 



have a problem with the way aqDateTime object adds a  month. 

Here is the code and the result: 




  var todayPlusMth = aqConvert.DateTimeToFormatStr(aqDateTime.AddMonths(aqDateTime.Today(), +1), "%#m/%#d/%Y");

  Log.Message(todayPlusMth);

  var tomorrow= aqConvert.DateTimeToFormatStr(aqDateTime.AddDays(aqDateTime.Today(), +1), "%#m/%#d/%Y");

  Log.Message(tomorrow);

  var tomorrowPlusMth = aqConvert.DateTimeToFormatStr(aqDateTime.AddMonths(tomorrow, +1), "%#m/%#d/%Y"); 

  Log.Message(tomorrowPlusMth);




Type Message Time Priority Has Picture Link

12/1/2011 15:47:10 Normal

11/1/2011 15:47:10 Normal

12/1/2011 15:47:10 Normal



Most likely this issue will not be there if tomorrow I will run the same script, but this is still an issue. 





Regards, 

Andrei 

4 Replies


  • Hi Andrei,



    We have reproduced the behavior with aqDateTime.AddMonths. We will investigate it and let you know our results as soon as we have some.








  • Hi Andrei,



    In the meantime, you can use the following script that counts the correct date by using the number of days in the current month:





    function Test1()

    {        

      var date = aqDateTime.SetDateElements(2011, 10, 31);

      var monthsCount = 1;

      if (monthsCount < 0)

      {

        Log.Message("You cannot specify a negative value");

        return;

      }

      var newDate = AddMonths(date, monthsCount); 

      Log.Message(date); 

      Log.Message("+ 1 month");

      Log.Message(newDate);

    }





    function AddMonths(date, monthsCount)

    {

      var monthsInLastYear = monthsCount % 12;

      var yearsCount = (monthsCount - monthsInLastYear) / 12;

      var year = aqDateTime.GetYear(date) + yearsCount; 

      var month = aqDateTime.GetMonth(date) + monthsInLastYear;

      if (12 < month) {

        year++;

        month = month % 12;

      }

      var daysCount = GetDaysCount(year, month);

      var day = aqDateTime.GetDay(date);

      if (day > daysCount) {

        var daysToEndOfMonth = GetDaysCount(aqDateTime.GetYear(date), aqDateTime.GetMonth(date)) - day;

        day = daysCount - daysToEndOfMonth;

      }

      var hours = aqDateTime.GetHours(date);

      var minutes = aqDateTime.GetMinutes(date);

      var seconds = aqDateTime.GetSeconds(date);

      return aqDateTime.SetDateTimeElements(year, month, day, hours, minutes, seconds);

    }





    function GetDaysCount(year, month)

    {

      var daysCount = 31;

      if (7 < month)

        month--;

      if (0 == month % 2) {

        if (2 != month) {

          daysCount = 30;

        }

        else {

          if (!aqDateTime.IsLeapYear(year)) {

            daysCount = 28;

          }

          else {

            daysCount = 29;

          }

        }

      }

      return daysCount;

    }










  • Hi Andrei,



    Thank you for reporting the problem. It will be fixed in a future major release of TestComplete.