MCPcopy
hub / github.com/callumalpass/tasknotes / parseDateToLocalInternal

Function parseDateToLocalInternal

src/utils/dateUtils.ts:23–221  ·  view source on GitHub ↗
(dateString: string)

Source from the content-addressed store, hash-verified

21const tasknotesLogger = createTaskNotesLogger({ tag: "Utils/DateUtils" });
22
23function parseDateToLocalInternal(dateString: string): Date {
24 if (!dateString) {
25 const error = new Error("Date string cannot be empty");
26 tasknotesLogger.error("Date parsing error:", {
27 category: "validation",
28 operation: "date-parsing",
29 details: { dateString },
30 error: error.message,
31 });
32 throw error;
33 }
34
35 // Trim whitespace
36 const trimmed = dateString.trim();
37
38 try {
39 // Handle date with day name format (e.g., "2024-01-26 Fri")
40 const dateWithDayNameMatch = trimmed.match(
41 /^(\d{4}-\d{2}-\d{2})\s+(Mon|Tue|Wed|Thu|Fri|Sat|Sun|Monday|Tuesday|Wednesday|Thursday|Friday|Saturday|Sunday)$/i
42 );
43 if (dateWithDayNameMatch) {
44 // Extract just the date part and continue with normal parsing
45 const dateOnly = dateWithDayNameMatch[1];
46 return parseDateToLocalInternal(dateOnly);
47 }
48
49 // Handle incomplete time format (e.g., "T00:00" without date)
50 if (trimmed.startsWith("T") && /^T\d{2}:\d{2}(:\d{2})?/.test(trimmed)) {
51 const error = new Error(`Invalid date format - time without date: ${dateString}`);
52 tasknotesLogger.warn("Date parsing error - incomplete time format:", {
53 category: "validation",
54 operation: "date-parsing-incomplete-time-format",
55 details: { original: dateString, trimmed },
56 error: error.message,
57 });
58 throw error;
59 }
60
61 // Handle ISO week format (e.g., "2025-W02")
62 if (/^\d{4}-W\d{2}$/.test(trimmed)) {
63 const [year, week] = trimmed.split("-W");
64 const yearNum = parseInt(year, 10);
65 const weekNum = parseInt(week, 10);
66
67 if (isNaN(yearNum) || isNaN(weekNum)) {
68 const error = new Error(`Invalid numeric values in ISO week format: ${dateString}`);
69 tasknotesLogger.warn("Date parsing error - invalid ISO week numbers:", {
70 category: "validation",
71 operation: "date-parsing-invalid-iso-week-numbers",
72 details: { original: dateString, year, week, yearNum, weekNum },
73 });
74 throw error;
75 }
76
77 if (weekNum < 1 || weekNum > 53) {
78 const error = new Error(
79 `Invalid week number in ISO week format: ${dateString} (week must be 1-53)`
80 );

Callers 15

parseDateToUTCFunction · 0.85
parseDateToLocalFunction · 0.85
parseDateFunction · 0.85
parseDateAsLocalFunction · 0.85
validateDateInputFunction · 0.85
addDaysToDateStringFunction · 0.85
addWeeksToDateStringFunction · 0.85
addMonthsToDateStringFunction · 0.85
addYearsToDateStringFunction · 0.85
formatDateForDisplayFunction · 0.85
getTimePartFunction · 0.85
formatDateTimeForDisplayFunction · 0.85

Calls 4

errorMethod · 0.80
warnMethod · 0.80
getDateMethod · 0.65
toStringMethod · 0.65

Tested by

no test coverage detected