MCPcopy Index your code
hub / github.com/callumalpass/tasknotes / formatDateForStorage

Function formatDateForStorage

src/utils/dateUtils.ts:1520–1549  ·  view source on GitHub ↗
(date: Date)

Source from the content-addressed store, hash-verified

1518 * @returns YYYY-MM-DD string representing the UTC calendar date
1519 */
1520export function formatDateForStorage(date: Date): string {
1521 try {
1522 // Validate input
1523 if (!date || !(date instanceof Date) || isNaN(date.getTime())) {
1524 tasknotesLogger.warn("formatDateForStorage received invalid date:", {
1525 category: "validation",
1526 operation: "formatdateforstorage-received-invalid-date",
1527 details: { value: date },
1528 });
1529 return "";
1530 }
1531
1532 // Use UTC methods to extract date components
1533 // This ensures the same date string regardless of user timezone
1534 const year = date.getUTCFullYear();
1535 const month = String(date.getUTCMonth() + 1).padStart(2, "0");
1536 const day = String(date.getUTCDate()).padStart(2, "0");
1537
1538 return `${year}-${month}-${day}`;
1539 } catch (error) {
1540 tasknotesLogger.error("Error formatting date for storage:", {
1541 category: "validation",
1542 operation: "formatting-date-storage",
1543 details: { date },
1544 error: error,
1545 });
1546 // Return empty string for invalid dates rather than potentially incorrect fallback
1547 return "";
1548 }
1549}
1550
1551/**
1552 * Generate UTC dates for calendar display, avoiding timezone issues

Calls 2

warnMethod · 0.80
errorMethod · 0.80

Tested by 2

addDaysFunction · 0.72
checkDateChangeFunction · 0.72