Forum Discussion

torus's avatar
torus
Contributor
2 years ago
Solved

keyword test; variable of type object; store DateTime in keyword variable - javascript

Hi, I am trying to store a COMDateTime object into a keyword local variable (as shown in the below pictures). Is this now allowed? When I run the below code, I get a 'type mismatch' error.

 

 

 

 

 

  • You will have to convert the timestamp to an integer value, which will represent number of seconds (Epoch)

    function main()
    {
        var time = timestamp(); // epoch time
        Log.Message("Epoch: " + time);
        
        var date = new Date(time); // date time
        Log.Message("Date: " + date);    
    }
    
    function timestamp()
    {
        var fileTimeStamp = aqFile.GetLastWriteTime("C:\\Temp\\Book1.xlsx");
        Log.Message("File: " + fileTimeStamp);
        var date = new Date(fileTimeStamp);
        return date.getTime(); // return epoch time
    }

     

5 Replies

  • rraghvani's avatar
    rraghvani
    Champion Level 3

    You will have to convert the timestamp to an integer value, which will represent number of seconds (Epoch)

    function main()
    {
        var time = timestamp(); // epoch time
        Log.Message("Epoch: " + time);
        
        var date = new Date(time); // date time
        Log.Message("Date: " + date);    
    }
    
    function timestamp()
    {
        var fileTimeStamp = aqFile.GetLastWriteTime("C:\\Temp\\Book1.xlsx");
        Log.Message("File: " + fileTimeStamp);
        var date = new Date(fileTimeStamp);
        return date.getTime(); // return epoch time
    }

     

  • torus's avatar
    torus
    Contributor

    thank you all for the help. Let me try this out and see if it works. 

  • torus's avatar
    torus
    Contributor

    You both are correct. It is unusual to me. I can store a Javascript Date into the object but I cannot store a TestComplete's aqDateTime into the object. 

     

     

     

      var date = aqDateTime.SetDateElements(year, month, day);
      var date2 = aqDateTime.AddDays(date, 2);
      var date2Str = aqConvert.DateTimeToStr(date2);
      var noww = new Date(date2Str);

     

     

    If I return the 'noww' variable (a Javascript Date object) to the keyword test, the keyword test will allow me to save the 'noww' value in a object variable. However! If I return date2 (a TestCompete Date object) to the keyword test, the keywod test will NOT allow me to save the returned 'date2' value to a keyword test's object variable. It is so unusual because it is their own object. I liked using the aqDateTime object because it was easy to manipulate the dates with it, but I'll use the old fashion Javascript Date object from now on. Thanks everyone!

    Both answers helped to get me pointed in a good direction. I guess I'll accept the one with code example in it.

    date2 is the TestComplete aqDateTime object, noww is the Javascript date object

     





  • torus's avatar
    torus
    Contributor

    Here is some code if anyone ever wants to know how to convert aqDateTime to javascript Date

     

     

    function AddDaysToToday(daysToAdd)
    {
      // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date 
      
      // Test Complete Date Object
      var date = aqDateTime.Now();
      date = aqDateTime.AddDays(date, daysToAdd);
      
      year = aqDateTime.GetYear(date);
      month = aqDateTime.GetMonth(date);
      day = aqDateTime.GetDay(date);
      
      // Javascript Date Object
      var jsDate = new Date(`${year}-${month}-${day}`);
    
      return jsDate;
    }

     

    Here are some other useful methods ... useful code snippets:

    function GetMonth_FromDate(jsDate)
    {
      // Month number starts at 0 for some reason
      var month = jsDate.getMonth() + 1;
      return month;
    }
    
    
    function GetDay_FromDate(jsDate)
    {
      var day = jsDate.getDate();
      return day;
    }
    
    
    function GetYear_FromDate(jsDate) 
    {
      var month = jsDate.getYear;
      return jsDate.getFullYear();
    }
    
    
    function AddDaysToJavascriptDate(jsDate, numOfDaysToAdd)
    {
      var dateModified = new Date(date); 
      dateModified.setDate(date.getDate() + numOfDaysToAdd);
    
      return dateModified;
    }