cancel
Showing results for 
Search instead for 
Did you mean: 

Change the / in aqDateTime.Today to -

SOLVED
Luukdb
New 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!

 

2 ACCEPTED SOLUTIONS

Accepted Solutions
tphillips
Community Leader

Re: Change the / in aqDateTime.Today to -

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.


--------------------
QA Systems Architect

View solution in original post

hkim5
Staff

Re: Change the / in aqDateTime.Today to -

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


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

ok- this is just a scripting question, which i think would be worthwhile going over. if you run the helper function "reformat_date" it takes in one parameter, which we are calling "current_datetime". This function (reformat_date) wants to take in the input of current_datetime (which we usually get by using the built in object/method of "aqDateTime.Today()" ) and then outputs (or "returns") the same date (which is our current date) in the format that we want with the "-" separators. This is why if you try to simply run the "reformat_date" function, you get an error, since you havent inputted any date in to reformat in the first place. 

Now the second function  above called "test", you can see that I've now added in a second line that reads 

 return reformat_date(aqDateTime.Today());

 such that as a result or running this script routine called "test" we not only log a message that reads the reformatted output of the current date (you can also observe here that the function "reformat_date" is now using the input parameter (of "aqDateTime.Today()" which holds our current date in the original format without the "-"). The second line outputs, as a result of running this "test" function, the current date in that same reformatted version with the "-" separators.

1. drag this "test" function in (as it is currently written, this will always grab the current date, reformat it, and return that reformatted version/ while also logging it as a message). 

2. drag in an on screen action, grab (drag and drop the blue target sign cursor thingy) the on screen element that you would like to type into.. the operation you want to use will most likely be "SetText" or "Keys" (usually the former). 

3. when it asks you what the value of the "SetText" operation should be, you can click the 3 dots, and point it to the "Last Operation Result". In which case this is referring to the result of the script routine "test" that we ran just prior to performing this on screen action. 

4. profit

Best,
Justin Kim

View solution in original post

10 REPLIES 10
hkim5
Staff

Re: Change the / in aqDateTime.Today to -

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
hkim5
Staff

Re: Change the / in aqDateTime.Today to -

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
Community Leader

Re: Change the / in aqDateTime.Today to -

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.


--------------------
QA Systems Architect

View solution in original post

Luukdb
New Contributor

Re: Change the / in aqDateTime.Today to -

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
Community Hero

Re: Change the / in aqDateTime.Today to -

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 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
================================
Luukdb
New Contributor

Re: Change the / in aqDateTime.Today to -

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
Community Leader

Re: Change the / in aqDateTime.Today to -

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.


--------------------
QA Systems Architect
hkim5
Staff

Re: Change the / in aqDateTime.Today to -

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
Luukdb
New Contributor

Re: Change the / in aqDateTime.Today to -

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?

New Here?
Join us and watch the welcome video:
Announcements