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

Function normalizeDateString

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

Source from the content-addressed store, hash-verified

433 * Normalize a date string to YYYY-MM-DD format for storage/comparison
434 */
435export function normalizeDateString(dateString: string): string {
436 if (!dateString) {
437 return dateString;
438 }
439
440 try {
441 // For timezone-aware strings, extract just the date part directly
442 if (
443 dateString.includes("T") ||
444 dateString.includes("Z") ||
445 dateString.match(/[+-]\d{2}:\d{2}$/)
446 ) {
447 // Extract date part before 'T' or timezone marker
448 const datePart = dateString.split("T")[0];
449 // Validate that it's a proper YYYY-MM-DD format
450 if (/^\d{4}-\d{2}-\d{2}$/.test(datePart)) {
451 return datePart;
452 }
453 }
454
455 // For non-timezone strings, parse with UTC anchor for consistent storage
456 const parsed = parseDateToUTC(dateString);
457 if (isValid(parsed)) {
458 return formatDateForStorage(parsed);
459 }
460 return dateString;
461 } catch (error) {
462 tasknotesLogger.error("Error normalizing date string:", {
463 category: "validation",
464 operation: "normalizing-date-string",
465 details: { dateString },
466 error: error,
467 });
468 return dateString; // Return original if parsing fails
469 }
470}
471
472/**
473 * Create a safe Date object for a specific year/month/day in local timezone

Callers 1

dateUtils.test.tsFile · 0.90

Calls 3

parseDateToUTCFunction · 0.85
formatDateForStorageFunction · 0.85
errorMethod · 0.80

Tested by

no test coverage detected