JavaScript - how to get current time but in different time zone(s)
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;
}
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 :) 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 :D Have a nice day.