Skip to content

Commit

Permalink
change time formatting logic
Browse files Browse the repository at this point in the history
  • Loading branch information
firehawk89 committed Sep 24, 2023
1 parent 9861adc commit 7d154f2
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/app/page.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export default function Home() {
const [timeStamp, setTimeStamp] = useState(0)

useEffect(() => {
setTimeStamp(Math.floor(Date.now() / 1000))
setTimeStamp(Date.now())
}, [])

return (
Expand Down
5 changes: 4 additions & 1 deletion src/components/message.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
<div className="flex items-end gap-2">
Expand Down
12 changes: 7 additions & 5 deletions src/utils/getFormattedDate.js
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit 7d154f2

Please sign in to comment.