Skip to content

Commit 43f1d5d

Browse files
authored
Merge pull request #4338 from traPtitech/show_full_date_of_message
traQ上のメッセージの投稿・編集日時に年月日を適宜省略しつつ付け足す
2 parents a3e2391 + 3480347 commit 43f1d5d

File tree

2 files changed

+47
-15
lines changed

2 files changed

+47
-15
lines changed

src/lib/basic/date.ts

+22-5
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,29 @@ export const getDateRepresentationWithoutSameDate = (
4242
}
4343

4444
export const getDisplayDate = (createdAt: string, updatedAt: string) => {
45-
const createdDate = new Date(createdAt)
46-
if (createdAt === updatedAt) {
47-
return getTimeString(createdDate)
45+
const displayDate = new Date(updatedAt)
46+
const today = new Date()
47+
const timeString = getTimeString(displayDate)
48+
const yesterday = new Date(today.getTime() - 1000 * 60 * 60 * 24)
49+
50+
if (
51+
displayDate.getFullYear() === today.getFullYear() &&
52+
displayDate.getMonth() === today.getMonth() &&
53+
displayDate.getDate() === today.getDate()
54+
) {
55+
return '今日' + ' ' + timeString
56+
}
57+
if (
58+
displayDate.getFullYear() === yesterday.getFullYear() &&
59+
displayDate.getMonth() === yesterday.getMonth() &&
60+
displayDate.getDate() === yesterday.getDate()
61+
) {
62+
return '昨日' + ' ' + timeString
63+
}
64+
if (displayDate.getFullYear() === today.getFullYear()) {
65+
return getDayString(displayDate) + ' ' + timeString
4866
} else {
49-
const updatedDate = new Date(updatedAt)
50-
return getDateRepresentationWithoutSameDate(updatedDate, createdDate)
67+
return getFullDayString(displayDate) + ' ' + timeString
5168
}
5269
}
5370

tests/unit/lib/basic/date.spec.ts

+25-10
Original file line numberDiff line numberDiff line change
@@ -94,18 +94,33 @@ describe('getDateRepresentationWithoutSameDate', () => {
9494
})
9595

9696
describe('getDisplayDate', () => {
97-
const dateISO = '2001-04-04T05:20:34'
98-
99-
it('should get time string when not modified', () => {
100-
expect(getDisplayDate(dateISO, dateISO)).toBe('05:20')
97+
beforeEach(() => {
98+
vi.useFakeTimers()
10199
})
102-
it('should get time string when same date', () => {
103-
const dateISO2 = '2001-04-04T08:25:34'
104-
expect(getDisplayDate(dateISO, dateISO2)).toBe('08:25')
100+
afterEach(() => {
101+
vi.useRealTimers()
105102
})
106-
it('should get date string when not same date', () => {
107-
const dateISO2 = '2001-06-04T08:25:34'
108-
expect(getDisplayDate(dateISO, dateISO2)).toBe('06/04 08:25')
103+
104+
const createdDateISO = '2010-04-01T12:34:56'
105+
const updatedDateISO = '2010-05-02T14:28:57'
106+
107+
it('should say 今日 when updated today', () => {
108+
vi.setSystemTime('2010-05-02T15:00:00')
109+
expect(getDisplayDate(createdDateISO, updatedDateISO)).toBe('今日 14:28')
110+
})
111+
it('should say 昨日 when updated yesterday', () => {
112+
vi.setSystemTime('2010-05-03T15:00:00')
113+
expect(getDisplayDate(createdDateISO, updatedDateISO)).toBe('昨日 14:28')
114+
})
115+
it('should get MM/DD when updated in the same year', () => {
116+
vi.setSystemTime('2010-07-07T15:00:00')
117+
expect(getDisplayDate(createdDateISO, updatedDateISO)).toBe('05/02 14:28')
118+
})
119+
it('should get YYYY/MM/DD when updated before last year', () => {
120+
vi.setSystemTime('2015-10-10T15:00:00')
121+
expect(getDisplayDate(createdDateISO, updatedDateISO)).toBe(
122+
'2010/05/02 14:28'
123+
)
109124
})
110125
})
111126

0 commit comments

Comments
 (0)