| 1518 | * @returns YYYY-MM-DD string representing the UTC calendar date |
| 1519 | */ |
| 1520 | export 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 |