Forum Discussion

levi_bryant's avatar
levi_bryant
Contributor
10 years ago
Solved

aqDateTime.GetDay may have a bug

I'm using TestComplete with JScript.



If I get the TimeInterval between two date/Time objects that are on the same day, aqDateTime.GetDay returns 30 instead of 0. If I get the interval between two date/time objects that are over a day apart it appears to return a closer approximation that is about 1 day off.



aqDateTime.GetHours, Minutes, and seconds appear to work fine.





Here is a sample piece of code showing this behavior.







function test()


{


var time1 = aqDateTime.Now();


Delay(1000);


var time2 = aqDateTime.Now();


time2 = aqDateTime.AddMinutes(time2,2);


time2 = aqDateTime.AddHours(time2,3);


 


var difference = aqDateTime.TimeInterval(time1,time2);


 


Log.Message(difference)


 


Log.Message(aqDateTime.GetSeconds(difference)) // Prints 1


 


Log.Message(aqDateTime.GetMinutes(difference)) // Prints 2


 


Log.Message(aqDateTime.GetHours(difference)) // Prints 3


 


Log.Message(aqDateTime.GetDay(difference)) // Prints 30 - BUG??


 


time2 = aqDateTime.AddDays(time2,4); // Adding 4 days


 


difference = aqDateTime.TimeInterval(time1,time2);


Log.Message(difference)


 


Log.Message(aqDateTime.GetDay(difference))// Prints 3 - Bug??


 


}


  • Short answer: It's by design. Use aqConvert.TimeIntervalToStr to get the day count for time intervals:



    var strDiff = aqConvert.TimeIntervalToStr(difference); // "0:03:02:01"

    Log.Message(strDiff.split(":")[0]); // Prints 0



    Long answer: Date-time values are stored as the time passed since December 30, 1899. Time intervals are date-time values too. So, intervals with 0-day difference map to the "zero day", December 30, 1899; with 1-day difference - to December 31, 1899; with 4-day difference - to January 3, 1900, and so on. GetDay treats values as dates, that's why it returns 30 and 3 in your example. The proper way to work with the days component of time internals is by using TimeIntervalToStr - it treats its parameter as a time span rather than a date.





    You may find the .NET DateTime more convenient to use for such things:



    function Test()

    {

      var time1 = dotNET.System.DateTime.Now();

      Delay(1000);

      var time2 = dotNET.System.DateTime.Now().AddHours(3).AddMinutes(2);



      var difference = time2.Subtract(time1);

      Log.Message(difference.Days); // 0



      time2 = time2.AddDays(4);

      difference = time2.Subtract(time1);



      Log.Message(difference.Days); // 4

    }

2 Replies

  • HKosova's avatar
    HKosova
    SmartBear Alumni (Retired)
    Short answer: It's by design. Use aqConvert.TimeIntervalToStr to get the day count for time intervals:



    var strDiff = aqConvert.TimeIntervalToStr(difference); // "0:03:02:01"

    Log.Message(strDiff.split(":")[0]); // Prints 0



    Long answer: Date-time values are stored as the time passed since December 30, 1899. Time intervals are date-time values too. So, intervals with 0-day difference map to the "zero day", December 30, 1899; with 1-day difference - to December 31, 1899; with 4-day difference - to January 3, 1900, and so on. GetDay treats values as dates, that's why it returns 30 and 3 in your example. The proper way to work with the days component of time internals is by using TimeIntervalToStr - it treats its parameter as a time span rather than a date.





    You may find the .NET DateTime more convenient to use for such things:



    function Test()

    {

      var time1 = dotNET.System.DateTime.Now();

      Delay(1000);

      var time2 = dotNET.System.DateTime.Now().AddHours(3).AddMinutes(2);



      var difference = time2.Subtract(time1);

      Log.Message(difference.Days); // 0



      time2 = time2.AddDays(4);

      difference = time2.Subtract(time1);



      Log.Message(difference.Days); // 4

    }

  • Thanks so much for your answer Helen!

    It was very informative.



    I did end up using the interval for the day just to make sure things worked until I heard back. I thought I found a bug but your explanation makes perfect sense now. The only thing that doesn't make sense is why they chose a date that would cause issues like that.

    Oh well. Thanks for the dotNet alternative!