Skip to content

Commit 47d8873

Browse files
committed
feat: new date rendering
Signed-off-by: Matt Gleich <git@mattglei.ch>
1 parent 69daed0 commit 47d8873

File tree

2 files changed

+21
-28
lines changed

2 files changed

+21
-28
lines changed

src/lib/time.ts

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,39 +2,32 @@ import type { Dayjs } from 'dayjs';
22
import dayjs from 'dayjs';
33
import duration from 'dayjs/plugin/duration';
44
import advancedFormat from 'dayjs/plugin/advancedFormat';
5+
import isSameOrAfter from 'dayjs/plugin/isSameOrAfter';
6+
import isSameOrBefore from 'dayjs/plugin/isSameOrBefore';
57

68
dayjs.extend(duration);
79
dayjs.extend(advancedFormat);
10+
dayjs.extend(isSameOrAfter);
11+
dayjs.extend(isSameOrBefore);
812

913
export function renderDate(date: Dayjs, now: Dayjs): string {
10-
const dayjsDate = dayjs(date);
11-
const yesterday = now.subtract(1, 'day');
12-
const tomorrow = now.add(1, 'day');
13-
let dayOfWeek: string;
14+
const d = dayjs(date);
1415

15-
if (
16-
now.date() === dayjsDate.date() &&
17-
now.year() === dayjsDate.year() &&
18-
now.month() === dayjsDate.month()
19-
) {
20-
dayOfWeek = 'Today';
21-
} else if (
22-
yesterday.date() === dayjsDate.date() &&
23-
yesterday.year() === dayjsDate.year() &&
24-
yesterday.month() === dayjsDate.month()
25-
) {
26-
dayOfWeek = 'Yesterday';
27-
} else if (
28-
tomorrow.date() === dayjsDate.date() &&
29-
tomorrow.year() === dayjsDate.year() &&
30-
tomorrow.month() === dayjsDate.month()
31-
) {
32-
dayOfWeek = 'Tomorrow';
16+
let label: string;
17+
18+
if (d.isSame(now, 'day')) {
19+
label = 'Today';
20+
} else if (d.isSame(now.subtract(1, 'day'), 'day')) {
21+
label = 'Yesterday';
22+
} else if (d.isSame(now.add(1, 'day'), 'day')) {
23+
label = 'Tomorrow';
24+
} else if (d.isSame(now, 'week')) {
25+
label = d.format('dddd');
3326
} else {
34-
dayOfWeek = dayjsDate.format('dddd, MMM Do');
27+
label = d.format('MMM Do');
3528
}
3629

37-
return dayjsDate.format(`[${dayOfWeek}] [at] h:mm A`);
30+
return `${label} at ${d.format('h:mm A')}`;
3831
}
3932

4033
export function fromNow(date: Dayjs, currentTime: Dayjs): string {
@@ -71,10 +64,10 @@ export function fromNow(date: Dayjs, currentTime: Dayjs): string {
7164
} else if (totalDaysDiff > 0) {
7265
fromNowStr = `${daysDiff} ${daysDiff === 1 ? 'day' : 'days'}`;
7366
if (hoursDiff > 0) {
74-
fromNowStr += ` ${hoursDiff}${hoursDiff === 1 ? 'hr' : 'hrs'}`;
67+
fromNowStr += ` ${hoursDiff}hr`;
7568
}
7669
} else if (hoursDiff > 0) {
77-
fromNowStr = `${hoursDiff}${hoursDiff === 1 ? 'hr' : 'hrs'}`;
70+
fromNowStr = `${hoursDiff}hr`;
7871
if (minutesDiff > 0) {
7972
fromNowStr += ` ${minutesDiff}m`;
8073
}

src/routes/writing/lcp/cache-status.svelte

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@
8383
8484
.refresh {
8585
padding: 15px 10px;
86-
padding-bottom: 10px;
86+
padding-bottom: 5px;
8787
display: flex;
8888
gap: 10px;
8989
align-items: center;
@@ -101,7 +101,7 @@
101101
102102
.updated {
103103
color: grey;
104-
padding-bottom: 5px;
104+
padding-bottom: 10px;
105105
display: flex;
106106
align-items: center;
107107
justify-content: center;

0 commit comments

Comments
 (0)