Skip to content

Commit 8028a97

Browse files
authored
Merge pull request #4 from maitamdev/feat/date-utils
feat(utils): add date utilities
2 parents 2e7bb5a + 4e70505 commit 8028a97

1 file changed

Lines changed: 44 additions & 0 deletions

File tree

src/utils/dateUtils.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Date utility functions
2+
3+
export function isToday(date: Date): boolean {
4+
const today = new Date();
5+
return date.getDate() === today.getDate() &&
6+
date.getMonth() === today.getMonth() &&
7+
date.getFullYear() === today.getFullYear();
8+
}
9+
10+
export function isThisWeek(date: Date): boolean {
11+
const now = new Date();
12+
const startOfWeek = new Date(now);
13+
startOfWeek.setDate(now.getDate() - now.getDay());
14+
startOfWeek.setHours(0, 0, 0, 0);
15+
const endOfWeek = new Date(startOfWeek);
16+
endOfWeek.setDate(startOfWeek.getDate() + 7);
17+
return date >= startOfWeek && date < endOfWeek;
18+
}
19+
20+
export function getGreeting(): string {
21+
const hour = new Date().getHours();
22+
if (hour < 12) return 'Good morning';
23+
if (hour < 17) return 'Good afternoon';
24+
return 'Good evening';
25+
}
26+
27+
export function getDaysUntil(target: Date): number {
28+
const now = new Date();
29+
const diff = target.getTime() - now.getTime();
30+
return Math.ceil(diff / (1000 * 60 * 60 * 24));
31+
}
32+
33+
export function getRelativeDate(date: Date): string {
34+
const now = new Date();
35+
const diffMs = now.getTime() - date.getTime();
36+
const diffMins = Math.floor(diffMs / 60000);
37+
const diffHours = Math.floor(diffMs / 3600000);
38+
const diffDays = Math.floor(diffMs / 86400000);
39+
if (diffMins < 1) return 'Just now';
40+
if (diffMins < 60) return `${diffMins}m ago`;
41+
if (diffHours < 24) return `${diffHours}h ago`;
42+
if (diffDays < 7) return `${diffDays}d ago`;
43+
return date.toLocaleDateString();
44+
}

0 commit comments

Comments
 (0)