JavaScript - how to get current time but in different time zone(s)

Originally posted by pkudrys on February 09, 2024

Hi,

I’m working on a project, where I need to get current time but in different time zone(s). I found that there is no way to achieve this with aqDateTime or aqConvert object. So I thought of some custom JavaScript code, but all of them are using 3rd party libs. I guess there is no way to import custom libs in TC? Does anyone know or use some kind of TimeZone-related JavaScript code, which works with TC? Thanks.

For example, I tried the below code, but of course it fails on ReferenceError: Intl is not defined.

function getSetActualDate(addDays, addMonths, addYears, dateFormat, timeZoneString) {
var timeUtc = new Date();
var timeZone = Intl.DateTimeFormat().resolvedOptions().timeZone;
var zoneTime = new Date(timeUtc.toLocaleString(timeZoneString, { timeZone }));

var adjustedTime = new Date(
zoneTime.getFullYear() + addYears,
zoneTime.getMonth() + addMonths,
zoneTime.getDate() + addDays,
zoneTime.getHours(),
zoneTime.getMinutes(),
zoneTime.getSeconds(),
zoneTime.getMilliseconds()
);

var dateString = adjustedTime.toLocaleString(timeZoneString, { timeZone, format: dateFormat });
return dateString;
}

Originally posted by rraghvani on February 09, 2024

This new forum, doesn’t seem to format the coding correctly. Are you able to edit, and upload the code again please?

Originally posted by pkudrys on February 09, 2024

Thanks for pointing this out :slight_smile: It’s fixed now.

Originally posted by rraghvani on February 09, 2024

Here’s a sample,

function DateTime()
{
    var event = new Date();
    Log.Message(event.toLocaleString('en-GB', {timeZone: 'UTC'}));
    Log.Message(event.toLocaleString('en-US', {timeZone: 'UTC'}));
    Log.Message(event.toLocaleString());
}

but it doesn’t seem to work in TC! However it works fine in https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleString.

Originally posted by pkudrys on February 09, 2024

Thanks for the sample. Unfortunately, as you found out, it doesn’t work in TC :frowning:

Another thing I tried is to use Moment Timezone library. I followed the steps mentioned here: Moment Timezone | Docs (momentjs.com)

And even though the Moment Timezone library was successfully installed by npm, TC returns “Unable to find the specified module” error.

I also tried to load the moment library dynamically, but it also fails with the same error…

I’m not entirely skilled in JavaScript, but I don’t se any obvious error. Am I doing something wrong or this approach is not supported by TC?

Originally posted by rraghvani on February 09, 2024

It’s not possible to do this in TC. You can only import test units.

See JavaScript - Specifics of Usage

Originally posted by pkudrys on February 15, 2024

Unfortunately, there seems to be no “simple” or built-in way how to get the actual time in different timezone. So I solved it by using powershell :slight_smile: Here is the method I’m using…

// Get actual date in defined format and Time Zone
function GetSetActualDate(dateFormat, timeZoneString) {

var oShell = getActiveXObject(“WScript.Shell”); // Or oShell = WshShell
var oExec = oShell.Exec(“powershell -command [System.TimeZoneInfo]::ConvertTime( (Get-Date), (Get-TimeZone -Id \"” + timeZoneString + “\")).ToString(\"” + dateFormat + “\")”);
oExec.StdIn.Close(); // Close standard input before reading output

// Get PowerShell output
var strOutput = oExec.StdOut.ReadAll();
// Trim leading and trailing empty lines
strOutput = aqString.Trim(strOutput, aqString.stAll);
return strOutput;
}

And this is how it looks in keyword test:

I still believe the TC should directly support INTL library (now unsupported in JavaScript) and dynamic loading of external libraries is a must in a modern TA tool! I will make a feature request for both issues. But of course, I’m not naive to think that they will be fixed anytime soon :smiley: Have a nice day.