Skip to content

Commit

Permalink
update date logic to account for timezones (#3840)
Browse files Browse the repository at this point in the history
  • Loading branch information
mozzius authored May 3, 2024
1 parent 051e897 commit a5511e3
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions src/components/dms/MessageItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -118,16 +118,15 @@ export function MessageItemMetadata({
}

// if in the last day
if (now.toISOString().slice(0, 10) === date.toISOString().slice(0, 10)) {
if (localDateString(now) === localDateString(date)) {
return time
}

// if yesterday
const yesterday = new Date(now)
yesterday.setDate(yesterday.getDate() - 1)
if (
yesterday.toISOString().slice(0, 10) === date.toISOString().slice(0, 10)
) {

if (localDateString(yesterday) === localDateString(date)) {
return _(msg`Yesterday, ${time}`)
}

Expand Down Expand Up @@ -164,3 +163,12 @@ export function MessageItemMetadata({
</TimeElapsed>
)
}

function localDateString(date: Date) {
// can't use toISOString because it should be in local time
const mm = date.getMonth()
const dd = date.getDate()
const yyyy = date.getFullYear()
// not padding with 0s because it's not necessary, it's just used for comparison
return `${yyyy}-${mm}-${dd}`
}

0 comments on commit a5511e3

Please sign in to comment.