Forum Discussion

pkudrys's avatar
pkudrys
Contributor
10 months ago

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.

  • rraghvani's avatar
    rraghvani
    Champion Level 3

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

  • 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.

  • Thanks for the sample. Unfortunately, as you found out, it doesn't work in TC :( 

    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?