From 7d154f29b81fd56b35970925421c5732ad2bd5fe Mon Sep 17 00:00:00 2001 From: Bochkovskyi Date: Sun, 24 Sep 2023 14:12:12 +0300 Subject: [PATCH] change time formatting logic --- src/app/page.jsx | 2 +- src/components/message.jsx | 5 ++++- src/utils/getFormattedDate.js | 12 +++++++----- 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/src/app/page.jsx b/src/app/page.jsx index de56d69..bf9548d 100644 --- a/src/app/page.jsx +++ b/src/app/page.jsx @@ -7,7 +7,7 @@ export default function Home() { const [timeStamp, setTimeStamp] = useState(0) useEffect(() => { - setTimeStamp(Math.floor(Date.now() / 1000)) + setTimeStamp(Date.now()) }, []) return ( diff --git a/src/components/message.jsx b/src/components/message.jsx index 9f1d55d..4913b09 100644 --- a/src/components/message.jsx +++ b/src/components/message.jsx @@ -3,7 +3,10 @@ import Image from 'next/image' import Link from 'next/link' export default function Message({ sentAt, children }) { - const time = getFormattedTime(sentAt) + const time = getFormattedTime(sentAt, { + hourOptions: '2-digit', + minuteOptions: '2-digit', + }) return (
diff --git a/src/utils/getFormattedDate.js b/src/utils/getFormattedDate.js index 96342cf..011444b 100644 --- a/src/utils/getFormattedDate.js +++ b/src/utils/getFormattedDate.js @@ -1,7 +1,9 @@ -export default function getFormattedTime(timestamp) { - const date = new Date(timestamp * 1000) - const hours = date.getHours() - const minutes = '0' + date.getMinutes() +export default function getFormattedTime(timestamp, options) { + const { format: formatTime } = new Intl.DateTimeFormat('en', { + hour: options.hourOptions, + minute: options.minuteOptions, + hour12: false, + }) - return hours + ':' + minutes.substr(-2) + return formatTime(timestamp) }