Current Time
{centralEuropeTime[0]}
-{centralEuropeTime[1]}
-{easternTime[0]}
-{easternTime[1]}
-diff --git a/src/components/content/DateTimeBox/index.tsx b/src/components/content/DateTimeBox/index.tsx index 056b26c97f..1f9b30250a 100644 --- a/src/components/content/DateTimeBox/index.tsx +++ b/src/components/content/DateTimeBox/index.tsx @@ -1,40 +1,53 @@ -import React from 'react'; +import React, { useState, useEffect } from 'react'; +import BrowserOnly from '@docusaurus/BrowserOnly'; -function DateTimeBox(): JSX.Element { - const date = new Date(); - const centralEuropeTime = [ - date.toLocaleString('en-US', { - timeZone: 'Europe/Paris', +function formatTime(timeZone: string): [string, string] { + const now = new Date(); + return [ + now.toLocaleString('en-US', { + timeZone, hour: 'numeric', minute: 'numeric', hour12: false, }), - Intl.DateTimeFormat('en-US', { timeZone: 'Europe/Paris', timeZoneName: 'long' }).format().split(',')[1], - ]; - const easternTime = [ - date.toLocaleString('en-US', { - timeZone: 'America/New_York', - hour: 'numeric', - minute: 'numeric', - hour12: false, - }), - Intl.DateTimeFormat('en-US', { timeZone: 'America/New_York', timeZoneName: 'long' }).format().split(',')[1], + Intl.DateTimeFormat('en-US', { timeZone, timeZoneName: 'long' }).format(now).split(',')[1], ]; +} + +function Clock(): JSX.Element { + const [centralEuropeTime, setCentralEuropeTime] = useState(() => formatTime('Europe/Paris')); + const [easternTime, setEasternTime] = useState(() => formatTime('America/New_York')); + + useEffect(() => { + const interval = setInterval(() => { + setCentralEuropeTime(formatTime('Europe/Paris')); + setEasternTime(formatTime('America/New_York')); + }, 60 * 1000); + return () => clearInterval(interval); + }, []); + return ( + <> +
{centralEuropeTime[1]}
+{easternTime[1]}
+{centralEuropeTime[1]}
-{easternTime[1]}
-