Skip to content

Commit 34656bf

Browse files
committed
fix: update isNew function to correctly determine if a post is new based on calendar days
1 parent b9745e3 commit 34656bf

2 files changed

Lines changed: 23 additions & 6 deletions

File tree

src/components/app/blog/utils.test.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,16 @@ describe("isNew", () => {
1616
expect(isNew("not-a-date")).toBe(false);
1717
});
1818

19-
test("returns true when post date is exactly now", () => {
19+
test("returns true when post date is earlier the same day", () => {
2020
vi.useFakeTimers();
2121
vi.setSystemTime(new Date("2026-03-27T12:00:00Z"));
22-
expect(isNew("2026-03-27")).toBe(true);
22+
expect(isNew("2026-03-27T10:00:00Z")).toBe(true);
23+
});
24+
25+
test("returns true when post date is yesterday (calendar day)", () => {
26+
vi.useFakeTimers();
27+
vi.setSystemTime(new Date("2026-03-27T12:00:00Z"));
28+
expect(isNew("2026-03-26")).toBe(true);
2329
});
2430

2531
test("returns true when post date is 23 hours ago", () => {
@@ -28,10 +34,10 @@ describe("isNew", () => {
2834
expect(isNew("2026-03-26T13:00:00Z")).toBe(true);
2935
});
3036

31-
test("returns false when post date is more than 24 hours ago", () => {
37+
test("returns false when post date is 2 or more calendar days ago", () => {
3238
vi.useFakeTimers();
3339
vi.setSystemTime(new Date("2026-03-27T12:00:00Z"));
34-
expect(isNew("2026-03-26T11:59:59Z")).toBe(false);
40+
expect(isNew("2026-03-25")).toBe(false);
3541
});
3642

3743
test("returns false when post date is in the future", () => {

src/components/app/blog/utils.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,19 @@ export const isNew = (rawDate?: string) => {
22
if (!rawDate) return false;
33
const postDate = new Date(rawDate);
44
if (Number.isNaN(postDate.getTime())) return false;
5-
const diffMs = Date.now() - postDate.getTime();
6-
return diffMs >= 0 && diffMs <= 24 * 60 * 60 * 1000;
5+
const postDay = Date.UTC(
6+
postDate.getUTCFullYear(),
7+
postDate.getUTCMonth(),
8+
postDate.getUTCDate()
9+
);
10+
const now = new Date();
11+
const today = Date.UTC(
12+
now.getUTCFullYear(),
13+
now.getUTCMonth(),
14+
now.getUTCDate()
15+
);
16+
const diffDays = (today - postDay) / (24 * 60 * 60 * 1000);
17+
return diffDays >= 0 && diffDays <= 1;
718
};
819

920
export const toDisplayDate = (rawDate?: string) => {

0 commit comments

Comments
 (0)