Patti Supply
/
Local Time

Local Time

Preview

Installation

Copy the element to Webflow

Copy to Webflow
Copied to clipboard!
{"type":"@webflow/XscpData","payload":{"nodes":[{"_id":"d03c4109-626e-93d2-6213-9e33df235d64","type":"Block","tag":"div","classes":["76a19c3e-5720-7ce0-f7b7-955958248015"],"children":["61efc499-4fd1-db8a-a31d-a2c1a2c4df8c","81bdb963-fb4a-f30e-a883-94c3002066d4"],"data":{"tag":"div","text":false,"xattr":[{"name":"data-p001-timezone","value":"Europe/Berlin"}],"devlink":{"runtimeProps":{},"slot":""},"displayName":"","attr":{"id":""},"search":{"exclude":false},"visibility":{"conditions":[],"keepInHtml":{"tag":"False","val":{}}}}},{"_id":"61efc499-4fd1-db8a-a31d-a2c1a2c4df8c","type":"DOM","tag":"div","classes":["a2500877-8ee7-87a3-7cd6-458529ef7924"],"children":["95ae82fb-f357-674f-0c85-897a638ba878"],"data":{"tag":"span","attributes":[{"name":"data-p001","value":"time"}],"text":false,"slot":"","visibility":{"conditions":[],"keepInHtml":{"tag":"False","val":{}}}}},{"_id":"95ae82fb-f357-674f-0c85-897a638ba878","text":true,"v":"15:32"},{"_id":"81bdb963-fb4a-f30e-a883-94c3002066d4","type":"DOM","tag":"div","classes":["80cf5f9c-2a89-57aa-b2a4-734640091eb7"],"children":["28484eb6-f327-a1ae-c2e3-c5012bfd23df"],"data":{"tag":"span","attributes":[{"name":"data-p001","value":"zone"}],"text":false,"slot":"","visibility":{"conditions":[],"keepInHtml":{"tag":"False","val":{}}}}},{"_id":"28484eb6-f327-a1ae-c2e3-c5012bfd23df","text":true,"v":"CET"}],"styles":[{"_id":"76a19c3e-5720-7ce0-f7b7-955958248015","fake":false,"type":"class","name":"p001-local-time","namespace":"","comb":"","styleLess":"display: flex; align-items: baseline; grid-column-gap: 1rem; grid-row-gap: 1rem;","variants":{},"children":[],"createdBy":"6a524a3cbc0fac0d25bf4b97","origin":null,"selector":null},{"_id":"a2500877-8ee7-87a3-7cd6-458529ef7924","fake":false,"type":"class","name":"p001-local-time_value","namespace":"","comb":"","styleLess":"color: hsla(0, 0.00%, 92.55%, 1.00); font-size: 4rem; line-height: 4rem;","variants":{},"children":[],"createdBy":"6a524a3cbc0fac0d25bf4b97","origin":null,"selector":null},{"_id":"80cf5f9c-2a89-57aa-b2a4-734640091eb7","fake":false,"type":"class","name":"p001-local-time_zone","namespace":"","comb":"","styleLess":"color: hsla(0, 0.00%, 92.67%, 1.00); font-size: 4rem; line-height: 4rem;","variants":{},"children":[],"createdBy":"6a524a3cbc0fac0d25bf4b97","origin":null,"selector":null}],"assets":[],"ix1":[],"ix2":{"interactions":[],"events":[],"actionLists":[]},"expandUserComponents":false},"meta":{"droppedLinks":0,"dynBindRemovedCount":0,"dynListBindRemovedCount":0,"paginationRemovedCount":0,"universalBindingsRemovedCount":0,"unlinkedSymbolCount":0,"codeComponentsRemovedCount":0,"richTextComponentsStripped":false,"sourceSiteId":"6b073cc8e349bca43fed7a2b"}}

Add custom JavaScript

JavaScript
(() => {
  const clocks = document.querySelectorAll('.p001-local-time');
  if (!clocks.length) return;
  const formatters = new Map();
  function updateClocks() {
    const now = new Date();
    clocks.forEach((clock) => {
      const time = clock.querySelector('[data-p001="time"]');
      const zone = clock.querySelector('[data-p001="zone"]');
      if (!time || !zone) return;
      // Use Berlin unless another timezone is specified.
      const timeZone = clock.getAttribute('data-p001-timezone') || 'Europe/Berlin';
      try {
        // Reuse the formatter for each timezone.
        if (!formatters.has(timeZone)) {
          formatters.set(
            timeZone,
            new Intl.DateTimeFormat(undefined, {
              timeZone,
              hour: '2-digit',
              minute: '2-digit',
              hourCycle: 'h23',
              timeZoneName: 'short',
            }),
          );
        }
        const parts = formatters.get(timeZone).formatToParts(now);
        const getPart = (type) => parts.find((part) => part.type === type)?.value || '';
        // Update the displayed time and timezone.
        time.textContent = `${getPart('hour')}:${getPart('minute')}`;
        zone.textContent = getPart('timeZoneName');
      } catch (error) {
        console.warn('P001: Invalid timezone:', timeZone, error);
      }
    });
    // Update at the next minute change.
    const delay = 60000 - (Date.now() % 60000) + 50;
    setTimeout(updateClocks, delay);
  }
  updateClocks();
})();

Add HTML structure

<div data-p001-timezone="Europe/Berlin" class="p001-local-time">
  <span data-p001="time" class="p001-local-time_value">--:--</span> <span data-p001="zone" class="p001-local-time_zone"></span>
</div>

‍

Add custom JavaScript

JavaScript
(() => {
  const clocks = document.querySelectorAll('.p001-local-time');
  if (!clocks.length) return;
  const formatters = new Map();
  function updateClocks() {
    const now = new Date();
    clocks.forEach((clock) => {
      const time = clock.querySelector('[data-p001="time"]');
      const zone = clock.querySelector('[data-p001="zone"]');
      if (!time || !zone) return;
      // Use Berlin unless another timezone is specified.
      const timeZone = clock.getAttribute('data-p001-timezone') || 'Europe/Berlin';
      try {
        // Reuse the formatter for each timezone.
        if (!formatters.has(timeZone)) {
          formatters.set(
            timeZone,
            new Intl.DateTimeFormat(undefined, {
              timeZone,
              hour: '2-digit',
              minute: '2-digit',
              hourCycle: 'h23',
              timeZoneName: 'short',
            }),
          );
        }
        const parts = formatters.get(timeZone).formatToParts(now);
        const getPart = (type) => parts.find((part) => part.type === type)?.value || '';
        // Update the displayed time and timezone.
        time.textContent = `${getPart('hour')}:${getPart('minute')}`;
        zone.textContent = getPart('timeZoneName');
      } catch (error) {
        console.warn('P001: Invalid timezone:', timeZone, error);
      }
    });
    // Update at the next minute change.
    const delay = 60000 - (Date.now() % 60000) + 50;
    setTimeout(updateClocks, delay);
  }
  updateClocks();
})();

Documentation

How it works

‍

Local Time displays the current time for a selected location using the browser's built-in Internationalization API. The time updates automatically and accounts for daylight saving time. The timezone can be added to the "data-p001-timezone" attribute. If no timezone is set "Europe/Berlin" will be used as default.

‍

‍

Configuration

‍

To display a different timezone, change the "data-p001-timezone" attribute to your timezone instead of "Europe/Berlin".

‍

Examples:

‍

  • America/New_York
  • Asia/Tokyo
  • Africa/Johannesburg

‍

You can find a list with all timezones here.

‍