Ask a Question

Change the / in aqDateTime.Today to -

SOLVED
Luukdb
Contributor

Change the / in aqDateTime.Today to -

Hello,

 

For my test I need to fill in the current date. But, our system doesn't allow the current date to be with / and it needs to be - instead. Does anyone know if it is possible to change this, and if it is, how can I change it?

 

I added screenshots for clearance. 

Luukdb_1-1613489343343.png

Luukdb_2-1613489407600.png

 

Thanks in advance!

 

10 REPLIES 10
hkim5
Staff

you would most likely need to create a little helper function that takes in that input date with "/" separators and replaces it with "-" instead. The syntax and the methods will differ based on the scripting language of choice, but for example, Python has the replace() method for strings, where you could switch out all "/" with "-"

 

 

Best,
Justin Kim

for example...

 

def change_date_format():
  day = str(aqDateTime.GetDay(aqDateTime.Now()))
  month = str(aqDateTime.GetMonth(aqDateTime.Now()))
  year = str(aqDateTime.GetYear(aqDateTime.Now()))
  list = [day, month, year]
  seperator = "-"
  inputdate = seperator.join(list)
  Log.Message(inputdate)
  return inputdate

and now you can drag and drop this on top of the keyword test operation where you are inputting the str value into your app, and then select the "last operation result" option instead.

 

Best,
Justin Kim
tphillips
Frequent Contributor

To avoid string splitting and weird edge cases, it would be worth looking into aqConvert.DateTimeToFormatStr() 

e.g:

 

 

 

function reformat_date(current_datetime) {
    return aqConvert.DateTimeToFormatStr(current_datetime, "%m-%d-%Y");
}

 

 

 

You can find the reference for date format specifiers here.

 

You could do this in a script, or even just use a code snippet operation.


--------------------
Senior SQA Automation Engineer

Thank you for the reply.

I am quite new to this, so I'm figuring things out. When I try to use your code I had to define current_datetime, and went searching in the links you provided. And tried to modify the code, but it's not working because it said that he is missing the reformat_date routine. 

 

I did put your code in the script so the routine has to be there. One of the things that might be the problem is that you cannot nest functions? When trying to separate the two functions he says that the current_datetime isn't defined.

 

function DateTimeToFormatStr()
{
  var current_datetime;
  
  current_datetime = aqDateTime.Today();


  function reformat_date(current_datetime) {
  
    return aqConvert.DateTimeToFormatStr(currrent_datetime, "%m-%d-%Y");

  }
}

 

AlexKaras
Champion Level 3

Hi,

 

Not sure that nested functions are supported.

Consider the code like this:

function DateTimeToFormatStr()
{
  var current_datetime;
  
  current_datetime = aqDateTime.Today();
  Log.Message(reformat_date(current_datetime));
}


function reformat_date(current_datetime) {
  return aqConvert.DateTimeToFormatStr(currrent_datetime, "%m-%d-%Y");
}

 

 

Regards,
  /Alex [Community Champion]
____
[Community Champions] 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 Champions]
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 Champion] signature is assigned on quarterly basis and is used with permission by SmartBear Software.
https://community.smartbear.com/t5/Community-Champions/About-the-Community-Champions-Program/gpm-p/252662
================================

I tried your code out but now he says bad variable type.

 

Luukdb_0-1613575337745.png

 

Does this mean that the log message isn't the input TestComplete wants?

tphillips
Frequent Contributor

I have no idea why it's not working for you.

 

Here's my code sample which works:

function reformat_date(current_datetime) {
    return aqConvert.DateTimeToFormatStr(current_datetime, "%m-%d-%Y");
}


function test() {
    Log.Message(reformat_date(aqDateTime.Today()));
}

tphillips_0-1613594074915.png

Note that you might need to switch around the format string to get day and month in the correct order.


--------------------
Senior SQA Automation Engineer

for clarity's sake,

 

copy and paste the entire snippet provided by @tphillips above, and run the function called "test".

This will take the parameter of the resulting value of aqDateTime.Now() as the input value to the help function we created called "reformat_date".

 

So that will output, with the "-" separators that we've defined, the current date... I think the date format that you want is actually %d-%m-%Y (so change to that in your reformat_date function, for the DateTimetoFormatStr() method).

Best,
Justin Kim

I used the snippet provided by @tphillips and running "test" the script works!

 

Now I'm trying to figure out how to get this value in my onscreen object. At first I thought to use the onscreen object method I can choose when running the reformat_date function.

Luukdb_0-1614007680641.png

But this results in an error.

Luukdb_1-1614007986118.png

So I tried to put the result in a string variable. And this gave no error but didn't put any data in the field.

Luukdb_2-1614008227814.png

Some time later I found a post about how to use returned results for scripts in the keyword test.
https://community.smartbear.com/t5/TestComplete-General-Discussions/how-to-use-returned-result-from-...

I tried this method by running the "test" function script and use the set variable to LastResult. This also didn't put data in the field.

Luukdb_3-1614008423045.png

 

Do you guys know what I am doing wrong?

cancel
Showing results for 
Search instead for 
Did you mean: