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

**URL:** <https://community.smartbear.com/t/javascript-how-to-get-current-time-but-in-different-time-zone-s/268>\
**Category:** TestComplete\
**Tags:** test, testcomplete, migrated, script-tests\
**Created:** [February 9, 2024, 8:24am UTC](https://community.smartbear.com/t/javascript-how-to-get-current-time-but-in-different-time-zone-s/268 "2024-02-09T08:24:49Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://avatars.discourse-cdn.com/v4/letter/s/bcef8e/32.png) [@system](https://community.smartbear.com/u/system)\
**Post date:** [February 9, 2024, 8:24am UTC](https://community.smartbear.com/t/javascript-how-to-get-current-time-but-in-different-time-zone-s/268/1 "2024-02-09T08:24:49Z")

</div>

> 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;  
> }

---

<div class="post-metadata">

**Author:** ![system](https://avatars.discourse-cdn.com/v4/letter/s/bcef8e/32.png) [@system](https://community.smartbear.com/u/system)\
**Post date:** [February 9, 2024, 10:03am UTC](https://community.smartbear.com/t/javascript-how-to-get-current-time-but-in-different-time-zone-s/268/2 "2024-02-09T10:03:15Z")

</div>

> 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?

---

<div class="post-metadata">

**Author:** ![system](https://avatars.discourse-cdn.com/v4/letter/s/bcef8e/32.png) [@system](https://community.smartbear.com/u/system)\
**Post date:** [February 9, 2024, 10:12am UTC](https://community.smartbear.com/t/javascript-how-to-get-current-time-but-in-different-time-zone-s/268/3 "2024-02-09T10:12:26Z")

</div>

> Originally posted by **pkudrys** on February 09, 2024

Thanks for pointing this out 🙂 It’s fixed now.

---

<div class="post-metadata">

**Author:** ![system](https://avatars.discourse-cdn.com/v4/letter/s/bcef8e/32.png) [@system](https://community.smartbear.com/u/system)\
**Post date:** [February 9, 2024, 12:07pm UTC](https://community.smartbear.com/t/javascript-how-to-get-current-time-but-in-different-time-zone-s/268/4 "2024-02-09T12:07:44Z")

</div>

> Originally posted by **rraghvani** on February 09, 2024

Here’s a sample,

```auto
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](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleString).

---

<div class="post-metadata">

**Author:** ![system](https://avatars.discourse-cdn.com/v4/letter/s/bcef8e/32.png) [@system](https://community.smartbear.com/u/system)\
**Post date:** [February 9, 2024, 2:08pm UTC](https://community.smartbear.com/t/javascript-how-to-get-current-time-but-in-different-time-zone-s/268/5 "2024-02-09T14:08:00Z")

</div>

> Originally posted by **pkudrys** on February 09, 2024

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)](https://momentjs.com/timezone/docs/#/use-it/node-js/)

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

 ![](https://canada1.discourse-cdn.com/flex049/uploads/smartbear_prod/original/1X/9059e6d17573620b0bb04978b7ccf55a8dab0a1e.png)

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

 ![](https://canada1.discourse-cdn.com/flex049/uploads/smartbear_prod/original/1X/ed1b377a1e9f3ff05a500c268e94064a28337dd3.png)

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?

---

<div class="post-metadata">

**Author:** ![system](https://avatars.discourse-cdn.com/v4/letter/s/bcef8e/32.png) [@system](https://community.smartbear.com/u/system)\
**Post date:** [February 9, 2024, 2:22pm UTC](https://community.smartbear.com/t/javascript-how-to-get-current-time-but-in-different-time-zone-s/268/6 "2024-02-09T14:22:56Z")

</div>

> 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](https://support.smartbear.com/testcomplete/docs/scripting/specifics/javascript.html)

---

<div class="post-metadata">

**Author:** ![system](https://avatars.discourse-cdn.com/v4/letter/s/bcef8e/32.png) [@system](https://community.smartbear.com/u/system)\
**Post date:** [February 15, 2024, 9:29am UTC](https://community.smartbear.com/t/javascript-how-to-get-current-time-but-in-different-time-zone-s/268/7 "2024-02-15T09:29:15Z")

</div>

> 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 🙂 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:

 ![](https://canada1.discourse-cdn.com/flex049/uploads/smartbear_prod/original/1X/a1af8b0a59382e74e35fbf8419bda709618f7350.png)

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 😃 Have a nice day.

---

<div class="post-metadata">

**Author:** ![system](https://avatars.discourse-cdn.com/v4/letter/s/bcef8e/32.png) [@system](https://community.smartbear.com/u/system)\
**Post date:** [October 7, 2026, 5:14pm UTC](https://community.smartbear.com/t/javascript-how-to-get-current-time-but-in-different-time-zone-s/268/8 "2026-10-07T17:14:46Z")

</div>

Closed on October 7, 2026, 5:14pm UTC

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.
