How to subtract to strings


In the picture I have saved the recieved date 21/02/2024 14:28 and timer ends at time 21/02/2024 14:43

These are date fields.

I use function

to get minuts. When I subtract i want the result to be 15 minutes.

However it does not work

if recived date is 10:58 og timer ends at time is 11:13.

System writes -45 Even it is 15 minutes. How would you guys write it in katalon recorder ??

Please help.

1 Like

Your result of -45 gives me thought that you should print out, or display, what two values you are working with. Are you assuming you are working with just your minutes or are you certain? Once we get you your “minutes”, then replacing the semi-colon with either a period or blank and subtracting the numbers is possible or there may be Date functions that already exist to do it.

What i do is that I have stored
21/02/2024 14:48 and 21/02/2024 15:03 as text. See first picture.
Then I use second picture to slice so i have for following:
a) 48 and 03 after slice ifunction s used.
This should give in time 15 minutes, but if i just subtract it gives 45. So is there a simple way to handle this time issue in katalon rec order ?

What happens when you flip the values around. So instead of storing:
21/02/2024 14:48 and 21/02/2024 15:03

you store the below and then do your calculations:
21/02/2024 15:03 and 21/02/2024 14:48


an error appears

Katalon Recorder would not help you much. You have to write good JavaScript codes to do that.

Are you capable of JavaScript programming? Do you know how to convert a string '21/02/2024 14:28' to a Date object? Do you know how to do subtraction operation between 2 Date objects?

Now you should quit Katalon Recorder. Just write a JavaScript script which starts as follows

const timerEndsAt1 = '21/02/2024 14:28'
const timerEndsAt2 = '21/02/2024 14:43'
...// more to be written

Please try to complete this script so that you get the result you want: '15'. Can you do it? If you can’t do it now, it would the best motivation for you to start learning JavaScript programming. Please do it first.

Once you could finished the js code, then you will transform the js script(s) into a format that Katalon Recorder requires. That could be done later.

I think here and now it will not solve my problem, but how will someone write it in katalon recorder with javascript code ??? Anyone ???

Let me share what I’ve done; what I found.

I wrote a set of scripts in JavaScript.

  • src/sample.js
import * as dtf from './custom_datetime_functions.js'

const timerEndsAt1 = '21/02/2024 14:28';
const timerEndsAt2 = '21/02/2024 15:01';

const diffMinutes2 = dtf.dateTimeDifferenceMinutes(timerEndsAt1, timerEndsAt2);
console.log(`difference: ${diffMinutes2} minutes`);
  • src/custom_datetime_functions.js
function toDateTime(timerEndsAt) {     // e.g., '21/02/2024 14:28'
  const re = new RegExp('([0-9]{2})/([0-9]{2})/([0-9]{4}) ([0-9]{2}):([0-9]{2})');
  const results = timerEndsAt.match(re);
  if (results) {
    const year = results[3] != null ? Number(results[3]) : 0;
    const monthMinus1 = results[2] != null ? Number(results[2]) : 0;
    const day = results[1] ? Number(results[1]) : 0;
    const hour = results[4] ? Number(results[4]) : 0;
    const minute = results[5] ? Number(results[5]) : 0;
    const date = new Date(year, monthMinus1, day, hour, minute);
    return date;
  } else {
    return null;
  }
}

function dateTimeDifferenceMinutes(timerEndsAt1, timerEndsAt2) {
  const dt1 = toDateTime(timerEndsAt1);
  const dt2 = toDateTime(timerEndsAt2);
  const diffMinutes = (dt2.getTime() - dt1.getTime()) / (1000 * 60);
  return diffMinutes;
}

export { toDateTime, dateTimeDifferenceMinutes }

In the command line, I ran the src/sample.js with Node.js

$ node src/sample.js
difference: 33 minutes

This worked. I could calculrate the date time difference between 21/02/2024 14:28 and 21/02/2024 15:01 is 33 minutes.

I think this is what @Amjad wants to see.

How can I ran the above mensioned JavaScript script and functions in Katalon Recorder?

To be honest, I don’t know. I could not find it out.


I found a stackoverflow topic, which suggest a way to define a JavaScript function and reuse it elsewhere in a Selenese command sequence:

I tried this way in Katalon Recorder but it never worked. The above stackoverflow post is dated 11 years ago. Probably this was talking about the previous version of Selenium IDE (Firefox Addon, already died).

I wrote a wrong information. Forgive me, please. Now I would say, I don’t know how to solve the @Amjad’s problem.

I am sure, I would be able to find an answer to this issue in Katalon Studio by scripting in Groovy language. But @Amjad wants Katalon Recorder. So it would not be satisfactory.

I would quit.

I think now are named extension scripts, if the current doc is correct:
Extension Scripts for Custom Locator Builders and Actions in Katalon Recorder | Katalon Docs.

the prototype for Actions may work

Thank you for the info. I read it and found something worth trying.

But I got a strange error in Katalon Recorder. It blocked me.

It seems that I encountered the problem as listed at

this issue is open. It seems that nobody is working on it. I am not keen on it as well.

I would quit again.

If I were to write test scripts in JavaScript/TypeScript, then Playwright is far easier for me to use.

By the way, I made a GitHub repository to study how to write code that scrapes 2 HTML element for date string like “21/02/2024 14:48” and “21/02/2024 15:03”; calculate the difference of these 2 datetime values, report the result in minutes unit.

This repository contains 3 sub-directories: webapp, test-katalonstudio and test-katalonrecorder

The webapp directory contains a React project of a web app which listens to http://localhost:3000 to show a web page like

The test-katalonstudio directory contains a Katalon Studio project. See Test Case/TC1

import org.openqa.selenium.WebDriver

import com.kazurayam.ks.driver.chrome4testing.ChromeForTestingDriverFactory
import com.kazurayam.ks.driver.chrome4testing.Installation
import com.kms.katalon.core.webui.driver.DriverFactory
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI
import com.kms.katalon.core.testobject.TestObject
import com.kms.katalon.core.testobject.ConditionType
import com.kazurayam.ks.time.UserExtensions
import java.time.LocalDateTime

TestObject makeTestObject(String id, String xpath) {
	TestObject tObj = new TestObject(id)
	tObj.addProperty("xpath", ConditionType.EQUALS, xpath)
	return tObj	
}

// specify the path information of browser and driver binaries
ChromeForTestingDriverFactory df =
	new ChromeForTestingDriverFactory(Installation.mac_116_0_5793_0_mac_x64)

// open a window of "Chrome for Testing"
WebDriver driver = df.newChromeForTestingDriver()

// tell Katalon Studio to use this WebDriver instance so that the WebUI keyword can work with it
DriverFactory.changeWebDriver(driver)

//-----------------------------------------------------------------------------

// your ordinary katalon life begins here

WebUI.navigateToUrl("http://localhost:3000/")

TestObject dt1Locator = makeTestObject("dt1", "//div[@id='e2d-officer_movements']/table/tbody/tr[1]/td[9]")
TestObject dt2Locator = makeTestObject("dt2", "//div[@id='e2d-officer_movements']/table/tbody/tr[2]/td[9]")
WebUI.verifyElementPresent(dt1Locator, 10);
WebUI.verifyElementPresent(dt2Locator, 10);
WebUI.comment("dt1=${WebUI.getText(dt1Locator)}")
WebUI.comment("dt2=${WebUI.getText(dt2Locator)}")

LocalDateTime ldt1 = UserExtensions.toLocalDateTime(WebUI.getText(dt1Locator))
LocalDateTime ldt2 = UserExtensions.toLocalDateTime(WebUI.getText(dt2Locator))

long diffMillis = UserExtensions.betweenMillis(ldt2, ldt1)
long diffSeconds = UserExtensions.betweenSeconds(ldt2, ldt1)
long diffMinutes = UserExtensions.betweenMinutes(ldt2, ldt1)
long diffHours = UserExtensions.betweenHours(ldt2, ldt1)
WebUI.comment("dt1 - dt2 in millis: ${diffMillis}")
WebUI.comment("dt1 - dt2 in seconds: ${diffSeconds}")
WebUI.comment("dt1 - dt2 in minutes: ${diffMinutes}")
WebUI.comment("dt1 - dt2 in hours: ${diffHours}")

WebUI.closeBrowser()

And “Include/scripts/groovy/com/kazurayam/ks/time/UserExtensions.groovy”

package com.kazurayam.ks.time

import java.time.LocalDateTime
import java.time.format.DateTimeFormatter
import java.time.temporal.ChronoUnit

public class UserExtensions {
	
	static DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyy HH:mm")
	
	static LocalDateTime toLocalDateTime(String text) {
		return LocalDateTime.parse(text, formatter)
	}
	
	static long betweenMillis(LocalDateTime start, LocalDateTime end) {
		return ChronoUnit.MILLIS.between(start, end)
	}
	
	static long betweenSeconds(LocalDateTime start, LocalDateTime end) {
		return betweenMillis(start, end) / (1000)
	}
	
	static long betweenMinutes(LocalDateTime start, LocalDateTime end) {
		return betweenMillis(start, end) / (1000 * 60)
	}
	
	static long betweenHours(LocalDateTime start, LocalDateTime end) {
		return betweenMillis(start, end) / (1000 * 60 * 60)
	}
}

When I ran the Test Case, I got the following result

I could successfully calculate the diff of 2 datetime instances in milliseconds, in seconds, in minuites, in hours. Was easy for me.

well, this is what i managed to find up to now.
but if the toy is broken and Katalon teams don’t care, why shoul we bother?
looks like this is just abandoned for my eyes and despite is opensourced i won’t spend anymore time on it.

I won’t spend anymore time on Katalon Recorder as well. Rather, I would go for Playwright to play on JavaScript/TypeScript.

i found a workaround

Just a question, on your line of “gotoif”, are you trying to state, “Not Equal”? If so, it likely should be !=
I have just never seen the symbol, !==, before.

when it is compared the lenght of the array it is used != operator and when it is comparing all elements of the array it is using !== operator .

1 Like