Ask a Question

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

SOLVED
ChristinaQa
Occasional Contributor

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.

 

store_dateTime_in_Object.PNG

 

 

store_dateTime_in_Object_2.PNG

 

 

store_dateTime_in_Object_3.PNG

5 REPLIES 5
Marsha_R
Community Hero

I don't seen COMDateTime as a variable type here

https://support.smartbear.com/testcomplete/docs/testing-with/variables/data-types/basic.html

 

Try specifically defining fileTimeStamp as a Date/Time type and see if that resolves the mismatch.

 

 


Marsha_R
[Community Hero]
____
[Community Heroes] are not employed by SmartBear Software but
are just volunteers who have some experience with the tools by SmartBear Software
and a desire to help others. Posts made by [Community Heroes]
may differ from the official policies of SmartBear Software and should be treated
as the own private opinion of their authors and under no circumstances as an
official answer from SmartBear Software.
The [Community Hero] signature is used with permission by SmartBear Software.
https://community.smartbear.com/t5/custom/page/page-id/hall-of-fame
rraghvani
Trusted Contributor

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
}

 

ChristinaQa
Occasional Contributor

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

ChristinaQa
Occasional 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

ChristinaQa_0-1679079550005.png

 





ChristinaQa
Occasional 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;
}

 

cancel
Showing results for 
Search instead for 
Did you mean: