-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.ts
More file actions
35 lines (29 loc) · 1.09 KB
/
Copy pathutils.ts
File metadata and controls
35 lines (29 loc) · 1.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// Minimal date helpers to avoid heavy dependency setup in this format
// In a real project, we would rely strictly on date-fns as requested.
// Here we wrap native implementation to ensure it runs standalone.
export const formatDate = (date: Date): string => {
const y = date.getFullYear();
const m = String(date.getMonth() + 1).padStart(2, '0');
const d = String(date.getDate()).padStart(2, '0');
return `${y}-${m}-${d}`;
};
export const parseDate = (dateStr: string): Date => {
if (!dateStr) return new Date();
const [y, m, d] = dateStr.split('-').map(Number);
return new Date(y, m - 1, d);
};
export const getTodayString = (): string => {
return formatDate(new Date());
};
export const addDays = (date: Date, days: number): Date => {
const result = new Date(date);
result.setDate(result.getDate() + days);
return result;
};
export const diffInDays = (d1: Date, d2: Date): number => {
const oneDay = 24 * 60 * 60 * 1000;
return Math.round((d1.getTime() - d2.getTime()) / oneDay);
};
export const generateId = (): string => {
return Math.random().toString(36).substring(2, 9);
};