|
| 1 | +export function dateToYMD(d) { |
| 2 | + const y = d.getFullYear(); |
| 3 | + const m = String(d.getMonth() + 1).padStart(2, '0'); |
| 4 | + const day = String(d.getDate()).padStart(2, '0'); |
| 5 | + return `${y}-${m}-${day}`; |
| 6 | +} |
| 7 | + |
| 8 | +export function parseDateRange(text) { |
| 9 | + const now = new Date(); |
| 10 | + const todayStr = dateToYMD(now); |
| 11 | + const lower = (text || '').toLowerCase(); |
| 12 | + let match; |
| 13 | + |
| 14 | + if ((match = lower.match(/last\s+(\d+)\s+days?/))) { |
| 15 | + const n = parseInt(match[1], 10); |
| 16 | + const start = new Date(); |
| 17 | + start.setDate(start.getDate() - (n - 1)); |
| 18 | + return { start: dateToYMD(start), end: todayStr, label: `Last ${n} days` }; |
| 19 | + } |
| 20 | + |
| 21 | + if ((match = lower.match(/last\s+(\d+)\s+weeks?/))) { |
| 22 | + const n = parseInt(match[1], 10); |
| 23 | + const start = new Date(); |
| 24 | + start.setDate(start.getDate() - (n * 7 - 1)); |
| 25 | + return { start: dateToYMD(start), end: todayStr, label: `Last ${n} weeks` }; |
| 26 | + } |
| 27 | + |
| 28 | + if (lower.includes('this week')) { |
| 29 | + const d = new Date(); |
| 30 | + const day = d.getDay(); |
| 31 | + const diff = (day + 6) % 7; |
| 32 | + const start = new Date(); |
| 33 | + start.setDate(start.getDate() - diff); |
| 34 | + return { start: dateToYMD(start), end: todayStr, label: 'This week' }; |
| 35 | + } |
| 36 | + |
| 37 | + if (lower.includes('last week')) { |
| 38 | + const currentWeekStart = new Date(); |
| 39 | + const day = currentWeekStart.getDay(); |
| 40 | + const diff = (day + 6) % 7; |
| 41 | + currentWeekStart.setDate(currentWeekStart.getDate() - diff); |
| 42 | + |
| 43 | + const end = new Date(currentWeekStart); |
| 44 | + end.setDate(end.getDate() - 1); |
| 45 | + |
| 46 | + const start = new Date(end); |
| 47 | + start.setDate(start.getDate() - 6); |
| 48 | + |
| 49 | + return { start: dateToYMD(start), end: dateToYMD(end), label: 'Last week' }; |
| 50 | + } |
| 51 | + |
| 52 | + if (lower.includes('this month')) { |
| 53 | + const start = new Date(now.getFullYear(), now.getMonth(), 1); |
| 54 | + return { start: dateToYMD(start), end: todayStr, label: 'This month' }; |
| 55 | + } |
| 56 | + |
| 57 | + if (lower.includes('last month')) { |
| 58 | + const start = new Date(now.getFullYear(), now.getMonth() - 1, 1); |
| 59 | + const end = new Date(now.getFullYear(), now.getMonth(), 0); |
| 60 | + return { start: dateToYMD(start), end: dateToYMD(end), label: 'Last month' }; |
| 61 | + } |
| 62 | + |
| 63 | + if (lower.includes('next month')) { |
| 64 | + const start = new Date(now.getFullYear(), now.getMonth() + 1, 1); |
| 65 | + const end = new Date(now.getFullYear(), now.getMonth() + 2, 0); |
| 66 | + return { start: dateToYMD(start), end: dateToYMD(end), label: 'Next month' }; |
| 67 | + } |
| 68 | + |
| 69 | + // month-year like "february 2006" or "feb 2006" |
| 70 | + if ((match = lower.match(/\b(january|february|march|april|may|june|july|august|september|october|november|december|jan|feb|mar|apr|may|jun|jul|aug|sep|sept|oct|nov|dec)\s+(\d{4})\b/))) { |
| 71 | + const monthStr = match[1]; |
| 72 | + const year = parseInt(match[2], 10); |
| 73 | + const months = { january:0, jan:0, february:1, feb:1, march:2, mar:2, april:3, apr:3, may:4, june:5, jun:5, july:6, jul:6, august:7, aug:7, september:8, sep:8, sept:8, october:9, oct:9, november:10, nov:10, december:11, dec:11 }; |
| 74 | + const mIdx = months[monthStr]; |
| 75 | + if (mIdx !== undefined && !isNaN(year)) { |
| 76 | + const start = new Date(year, mIdx, 1); |
| 77 | + const end = new Date(year, mIdx + 1, 0); |
| 78 | + const label = `${monthStr.charAt(0).toUpperCase()}${monthStr.slice(1)} ${year}`; |
| 79 | + return { start: dateToYMD(start), end: dateToYMD(end), label }; |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + // explicit YYYY-MM-DD - YYYY-MM-DD or YYYY-MM-DD to YYYY-MM-DD |
| 84 | + if ((match = text.match(/(\d{4}-\d{2}-\d{2})(?:\s*-\s*|\s+to\s+)(\d{4}-\d{2}-\d{2})/))) { |
| 85 | + return { start: match[1], end: match[2], label: `${match[1]} → ${match[2]}` }; |
| 86 | + } |
| 87 | + |
| 88 | + // explicit like "Apr 1 - Apr 5" or "Apr 1 to Apr 5" |
| 89 | + if ((match = text.match(/([A-Za-z]{3,}\s+\d{1,2}(?:,\s*\d{4})?)(?:\s*-\s*|\s+to\s+)([A-Za-z]{3,}\s+\d{1,2}(?:,\s*\d{4})?)/))) { |
| 90 | + const s = new Date(match[1]); |
| 91 | + const e = new Date(match[2]); |
| 92 | + if (!isNaN(s) && !isNaN(e)) { |
| 93 | + return { start: dateToYMD(s), end: dateToYMD(e), label: `${match[1]} → ${match[2]}` }; |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + // year-only like "2006" |
| 98 | + if ((match = lower.match(/\b(\d{4})\b/))) { |
| 99 | + const y = parseInt(match[1], 10); |
| 100 | + const start = new Date(y, 0, 1); |
| 101 | + const end = new Date(y, 11, 31); |
| 102 | + return { start: dateToYMD(start), end: dateToYMD(end), label: `${y}` }; |
| 103 | + } |
| 104 | + |
| 105 | + // month-only like "february" or "feb" (assume current year) |
| 106 | + if ((match = lower.match(/\b(january|february|march|april|may|june|july|august|september|october|november|december|jan|feb|mar|apr|may|jun|jul|aug|sep|sept|oct|nov|dec)\b/))) { |
| 107 | + const monthStr = match[1]; |
| 108 | + const months = { january:0, jan:0, february:1, feb:1, march:2, mar:2, april:3, apr:3, may:4, june:5, jun:5, july:6, jul:6, august:7, aug:7, september:8, sep:8, sept:8, october:9, oct:9, november:10, nov:10, december:11, dec:11 }; |
| 109 | + const mIdx = months[monthStr]; |
| 110 | + if (mIdx !== undefined) { |
| 111 | + const start = new Date(now.getFullYear(), mIdx, 1); |
| 112 | + const end = new Date(now.getFullYear(), mIdx + 1, 0); |
| 113 | + const label = `${monthStr.charAt(0).toUpperCase()}${monthStr.slice(1)} ${now.getFullYear()}`; |
| 114 | + return { start: dateToYMD(start), end: dateToYMD(end), label }; |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + const start = new Date(); |
| 119 | + start.setDate(start.getDate() - 6); |
| 120 | + return { start: dateToYMD(start), end: todayStr, label: 'Last 7 days' }; |
| 121 | +} |
0 commit comments