# Digital real-time clock \*\*with specific time zone\*\*

**URL:** <https://forum.wixstudio.com/t/digital-real-time-clock-with-specific-time-zone/63621>\
**Category:** Ask the community\
**Tags:** code, wix-studio, question\
**Created:** [October 16, 2024, 6:17pm UTC](https://forum.wixstudio.com/t/digital-real-time-clock-with-specific-time-zone/63621 "2024-10-16T18:17:18Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Allison\_Cressey](https://avatars.discourse-cdn.com/v4/letter/a/e19adc/32.png) [@Allison\_Cressey](https://forum.wixstudio.com/u/Allison_Cressey)\
**Post date:** [October 16, 2024, 6:17pm UTC](https://forum.wixstudio.com/t/digital-real-time-clock-with-specific-time-zone/63621/1 "2024-10-16T18:17:18Z")

</div>

I’d like to add a real-time **live text** clock:

- 24-hour
- AKST (Alaska standard time zone only)
- digital clock
- Styled to match my p3 header element.

Can anyone help me with how-to code and implement this?

The final display would read like below:

12:58:01 AKST

---

<div class="post-metadata">

**Author:** ![onemoretime](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/onemoretime/32/104023_2.png) [@onemoretime](https://forum.wixstudio.com/u/onemoretime)\
**Post date:** [October 17, 2024, 1:35am UTC](https://forum.wixstudio.com/t/digital-real-time-clock-with-specific-time-zone/63621/2 "2024-10-17T01:35:01Z")

</div>

Hi, Allison\_Cressey !!

In simple programming tasks related to time, it’s often faster and more accurate to use AI to put together the code. So this time, I leveraged the help of AI to assemble the code.

```javascript
$w.onReady(function () {

  // Initial execution and update every second
  setInterval(updateClock, 1000); // Runs every 1 second
  updateClock(); // Immediate display when the page loads

});

function updateClock() {

    // Alaska Standard Time (AKST) is UTC-9 hours
    const akstOffset = -9;
    
    // Apply Alaska Standard Time offset to UTC time
    const akstTime = new Date(Date.now() + (akstOffset * 60 * 60 * 1000));

    // Get hours, minutes, and seconds, and pad with zeros if needed
    const hours = String(akstTime.getUTCHours()).padStart(2, '0');
    const minutes = String(akstTime.getUTCMinutes()).padStart(2, '0');
    const seconds = String(akstTime.getUTCSeconds()).padStart(2, '0');

    // Format the time string for display
    const timeString = `${hours}:${minutes}:${seconds} AKST`;

    // Display the clock in yourClockTextElem
    $w("#yourClockTextElemID").text = timeString;
	
}

```

I checked from my environment (Japan Time Zone), and with this code, the time in Alaska was displayed correctly. (However, there seems to be a one-hour difference due to daylight saving time at the moment.) While I think there are no major issues with this code, there are a few points that I believe are worth noting.

First, the time that can be retrieved from frontend code depends on the device’s internal clock, so if the user’s device clock is off, the calculated Alaska time could also be inaccurate. To address this, it may be necessary to communicate with a server that provides the correct UTC time values. (A simple solution might be to return the value from Wix’s backend code.)

Also, in the code, you’re using `setInterval` for repeated execution. If you want to make the clock run more accurately, you could adjust the interval to a shorter value (currently set to 1000ms, but you could try something like 100ms). Lastly, regarding the string “AKST” at the end, because the width of the time text fluctuates, it may move slightly. I recommend displaying this string in a separate text element to avoid that issue. 🤔

※ If you want a clock that can automatically handle daylight saving time (DST) and other adjustments, introducing a date library like `date-fns-tz` will allow for more stable time display.

```javascript
import { toZonedTime, format } from 'date-fns-tz';

$w.onReady(function () {

  // Initial execution and update every second
  setInterval(updateClock, 1000); // Runs every 1 second
  updateClock(); // Immediate display when the page loads

});

function updateClock() {

    const alaskaTimezone = 'America/Anchorage';
    const date = new Date();
    const alaskaTime = toZonedTime(date, alaskaTimezone);
    const timeString = format(alaskaTime, 'HH:mm:ss zzzz', { timeZone: alaskaTimezone });
    $w("#yourClockTextElemID").text = timeString;

}

```

---

<div class="post-metadata">

**Author:** ![Allison\_Cressey](https://avatars.discourse-cdn.com/v4/letter/a/e19adc/32.png) [@Allison\_Cressey](https://forum.wixstudio.com/u/Allison_Cressey)\
**Post date:** [October 17, 2024, 3:32pm UTC](https://forum.wixstudio.com/t/digital-real-time-clock-with-specific-time-zone/63621/3 "2024-10-17T15:32:33Z")

</div>

Thank you so much! How do I implement this code to the page?

I’ve added the script to my page code, but not sure what to do next??

---

<div class="post-metadata">

**Author:** ![onemoretime](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/onemoretime/32/104023_2.png) [@onemoretime](https://forum.wixstudio.com/u/onemoretime)\
**Post date:** [October 17, 2024, 3:55pm UTC](https://forum.wixstudio.com/t/digital-real-time-clock-with-specific-time-zone/63621/4 "2024-10-17T15:55:29Z")

</div>

Hi, Allison !

First, please add a text element to the page. Then, select the text element and open the coding panel. Look at the “Properties & Events” section on the right side. There, you will find a field labeled “ID,” so replace it with “yourClockTextElemID” (you can change this string to any name you like, but make sure it matches the name in your code). After that, you should be able to preview the site or check the live site to confirm that the clock is working. 😉

By the way, if you want to run the second code, you’ll need to install the “date-fns-tz” library on your site via npm in advance. For now, try running the first code to see the code in action. :+1:

---

<div class="post-metadata">

**Author:** ![Allison\_Cressey](https://avatars.discourse-cdn.com/v4/letter/a/e19adc/32.png) [@Allison\_Cressey](https://forum.wixstudio.com/u/Allison_Cressey)\
**Post date:** [October 17, 2024, 4:01pm UTC](https://forum.wixstudio.com/t/digital-real-time-clock-with-specific-time-zone/63621/5 "2024-10-17T16:01:41Z")

</div>

Got the first code to work! It’s an hour off like you mentioned, so I need to install this library after all. Appreciate all your help – how would I go about adding this library function?

---

<div class="post-metadata">

**Author:** ![onemoretime](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/onemoretime/32/104023_2.png) [@onemoretime](https://forum.wixstudio.com/u/onemoretime)\
**Post date:** [October 17, 2024, 4:20pm UTC](https://forum.wixstudio.com/t/digital-real-time-clock-with-specific-time-zone/63621/6 "2024-10-17T16:20:20Z")

</div>

For installation instructions, please refer to this article. You should be able to find it by searching for "date-fns-tz. 😉

> **[Work with npm Packages](https://dev.wix.com/docs/develop-websites/articles/coding-with-velo/packages/work-with-npm-packages)**
>
> Wix allows you to install public npm packages directly from the npm registry to your site. This article explains how to install, use, and uninstall npm packages, which can be done in the editor (Wix Studio and Wix Editor), or by using the Wix CLI...

---

<div class="post-metadata">

**Author:** ![Allison\_Cressey](https://avatars.discourse-cdn.com/v4/letter/a/e19adc/32.png) [@Allison\_Cressey](https://forum.wixstudio.com/u/Allison_Cressey)\
**Post date:** [October 17, 2024, 4:43pm UTC](https://forum.wixstudio.com/t/digital-real-time-clock-with-specific-time-zone/63621/7 "2024-10-17T16:43:47Z")

</div>

Ok figured out how to install the npm… this is the file i installed under the packages in wix, but now it’s not working ☹

> **[date-fns-tz](https://www.npmjs.com/package/date-fns-tz)**
>
> Time zone support for date-fns v3 with the Intl API. Latest version: 3.2.0, last published: 17 days ago. Start using date-fns-tz in your project by running \`npm i date-fns-tz\`. There are 1267 other projects in the npm registry using date-fns-tz.

---

<div class="post-metadata">

**Author:** ![Allison\_Cressey](https://avatars.discourse-cdn.com/v4/letter/a/e19adc/32.png) [@Allison\_Cressey](https://forum.wixstudio.com/u/Allison_Cressey)\
**Post date:** [October 17, 2024, 4:48pm UTC](https://forum.wixstudio.com/t/digital-real-time-clock-with-specific-time-zone/63621/8 "2024-10-17T16:48:01Z")

</div>

\*\*Right now I’m just seeing the text element render “00:00:00”

* * *

Here’s the code I have input:

```javascript
import { toZonedTime, format } from 'date-fns-tz';

$w.onReady(function () {

  // Initial execution and update every second
  setInterval(updateClock, 1000); // Runs every 1 second
  updateClock(); // Immediate display when the page loads

});

function updateClock() {

    const alaskaTimezone = 'America/Anchorage';
    const date = new Date();
    const alaskaTime = toZonedTime(date, alaskaTimezone);
    const timeString = format(alaskaTime, 'HH:mm:ss zzzz', { timeZone: alaskaTimezone });
    $w("#yourClockTextElemID").text = timeString;

}

```

![Screen Shot 2024-10-17 at 9.47.05 AM](https://us1.discourse-cdn.com/wix/original/3X/a/8/a8b2b9510baba2a20f10eab492f89e2fe4c61f6c.png)

---

<div class="post-metadata">

**Author:** ![onemoretime](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/onemoretime/32/104023_2.png) [@onemoretime](https://forum.wixstudio.com/u/onemoretime)\
**Post date:** [October 17, 2024, 5:19pm UTC](https://forum.wixstudio.com/t/digital-real-time-clock-with-specific-time-zone/63621/9 "2024-10-17T17:19:25Z")

</div>

Hmm, I wonder why? I tried it just now, and it worked.

For now, please try rewriting it like the following. 🤔

```javascript
const timeString = format(alaskaTime, 'HH:mm:ss', { timeZone: alaskaTimezone });

```

If the issue is not resolved…

Next, after rewriting the code like this, please open the live site and check how the logs are displayed in the browser’s developer tools.

```javascript
function updateClock() {

    const alaskaTimezone = 'America/Anchorage';
    const date = new Date();
    console.log("Current local date:", date); 

    const alaskaTime = toZonedTime(date, alaskaTimezone);
    console.log("Alaska time (toZonedTime):", alaskaTime);

    const timeString = format(alaskaTime, 'HH:mm:ss zzzz', { timeZone: alaskaTimezone });
    console.log("Formatted time string:", timeString);

    $w("#yourClockTextElemID").text = timeString;

}

```
