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 c...
  • pkudrys's avatar
    10 months ago

    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.