Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions cypress/unit/formatDateAndTime.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ describe('formatDateAndTime', () => {
it('should format the date correctly', () => {
const utcTimestamp = '2023-01-01T12:00:00Z'
const formatted = formatDateAndTime(utcTimestamp)
expect(formatted).to.eq('Jan 1 4:00 AM')
expect(formatted).to.eq('Jan 1, 4:00 AM')
})

it('should include year when the time difference is more than 365 days', () => {
it('should include the year when showing a date from a different year', () => {
const utcTimestamp = '2022-01-01T12:00:00Z'
const formatted = formatDateAndTime(utcTimestamp)
expect(formatted).to.eq('Jan 1 2022 4:00 AM')
expect(formatted).to.eq('Jan 1, 2022 at 4:00 AM')
})
})
2 changes: 1 addition & 1 deletion cypress/unit/formatMessageTimestamp.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ describe('formatTimestamp', () => {

it('should return date and time when time difference is more than 24 hours', () => {
const utcTimestamp = '2021-01-01T00:00:00Z'
const expected = 'Dec 31 2020 4:00 PM'
const expected = 'Dec 31, 2020 at 4:00 PM'
const result = formatMessageTimestamp(utcTimestamp)
expect(result).to.eq(expected)
})
Expand Down
24 changes: 16 additions & 8 deletions src/components/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,31 @@ export function calculateTimeAgo(isoDate: string): string {
export function formatDateAndTime(isoDateTimeInUtc: string): string {
const convertedDate = new Date(isoDateTimeInUtc)
const userLocale = navigator.language
const options: Intl.DateTimeFormatOptions = {
const dateOptions: Intl.DateTimeFormatOptions = {
month: 'short',
day: 'numeric',
}

const timeOptions: Intl.DateTimeFormatOptions = {
hour: 'numeric',
minute: '2-digit',
hour12: true,
}

const timeDifferenceInDays =
calculateTimeDiffInSeconds(isoDateTimeInUtc) / (60 * 60 * 24)
const currentDate = new Date()
const isDifferentYear =
convertedDate.getFullYear() !== currentDate.getFullYear()

if (timeDifferenceInDays > 365) {
options.year = 'numeric'
const dateAndTimeConnector = isDifferentYear ? ' at ' : ', '

if (isDifferentYear) {
dateOptions.year = 'numeric'
}
const formattedDateTime = convertedDate
.toLocaleTimeString(userLocale, options)
.replace(/,/g, '')

const formattedDateTime =
convertedDate.toLocaleDateString(userLocale, dateOptions) +
dateAndTimeConnector +
convertedDate.toLocaleTimeString(userLocale, timeOptions)
return formattedDateTime
}

Expand Down
8 changes: 4 additions & 4 deletions src/components/messageCanvas/messageCanvas.cy.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ describe('MessageCanvas', () => {
<p>Hello World</p>
</MessageCanvas>
)
cy.contains('Jan 1 2020').should('not.be.visible')
cy.contains('Jan 1, 2020').should('not.be.visible')
cy.get('.rustic-message-canvas').realHover()
cy.contains('Jan 1 2020').should('be.visible')
cy.contains('Jan 1, 2020').should('be.visible')
})

it('shows that it was last updated if an update is provided', () => {
Expand All @@ -87,9 +87,9 @@ describe('MessageCanvas', () => {
<p>Hello World</p>
</MessageCanvas>
)
cy.contains('Jan 1 2020').should('not.be.visible')
cy.contains('Jan 1, 2020').should('not.be.visible')
cy.get('.rustic-message-canvas').realTouch()
cy.contains('Jan 1 2020').should('be.visible')
cy.contains('Jan 1, 2020').should('be.visible')
})

it('shows that it was last updated if an update is provided', () => {
Expand Down
25 changes: 23 additions & 2 deletions src/components/timestamp/timestamp.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,36 @@ export default {
},
}

const newDate = new Date()
function getYesterdayDate() {
const today = new Date()
const yesterday = new Date(today)
yesterday.setDate(today.getDate() - 1)
return yesterday
}

const yesterday = getYesterdayDate()
const now = new Date()

export const Default = {
args: {
timestamp: '2023-11-26T23:25:44Z',
},
}

export const OverADayAgo = {
args: {
timestamp: yesterday,
},
}

export const NotThisYear = {
args: {
timestamp: '2022-11-26T23:25:44Z',
},
}

export const TimeAgo = {
args: {
timestamp: newDate,
timestamp: now,
},
}