Skip to content

Fix date offset bugs in calendar drag&drop and task creation - #1

Draft
bata-san with Copilot wants to merge 2 commits into
mainfrom
copilot/fix-9ee53632-be58-4666-8864-5ea40d5ada69
Draft

Fix date offset bugs in calendar drag&drop and task creation#1
bata-san with Copilot wants to merge 2 commits into
mainfrom
copilot/fix-9ee53632-be58-4666-8864-5ea40d5ada69

Conversation

Copilot AI commented Jul 1, 2025

Copy link
Copy Markdown

Problem

The application had date offset bugs where tasks were being created 1 day earlier than intended in two scenarios:

  1. Calendar drag & drop: When dragging tasks to specific calendar dates, tasks were created on the previous day
  2. New task creation: When selecting dates for new tasks, they were saved one day before the selected date

Root Cause

The issue was caused by timezone handling problems when converting between JavaScript Date objects and date strings:

// Problematic code that caused date shifts:
const newStartDate = event.start.toISOString().split('T')[0];
const taskDate = dates[0].toISOString().split('T')[0];

When Date objects are created from date-only strings (like "2024-01-15"), JavaScript interprets them as UTC midnight. In timezones behind UTC, calling toISOString() would shift the date to the previous day.

Solution

Added timezone-safe utility functions and updated all date handling code:

New Utility Functions

function getLocalDateString(date) {
    // Safely converts any date input to YYYY-MM-DD format in local timezone
    if (!date) return new Date().toLocaleDateString('sv-SE');
    
    if (typeof date === 'string') {
        if (/^\d{4}-\d{2}-\d{2}$/.test(date)) return date;
        date = new Date(date + 'T00:00:00'); // Local time interpretation
    }
    
    if (date instanceof Date && !isNaN(date)) {
        return date.toLocaleDateString('sv-SE'); // sv-SE gives YYYY-MM-DD format
    }
    
    return new Date().toLocaleDateString('sv-SE');
}

function createLocalDate(dateString) {
    // Creates local Date objects from YYYY-MM-DD strings without timezone shifts
    if (/^\d{4}-\d{2}-\d{2}$/.test(dateString)) {
        return new Date(dateString + 'T00:00:00'); // Force local interpretation
    }
    return new Date(dateString + 'T00:00:00');
}

Fixed Functions

Calendar Drag & Drop:

// Before (buggy):
const newStartDate = event.start.toISOString().split('T')[0];
const newEndDate = new Date(event.end.getTime() - 86400000).toISOString().split('T')[0];

// After (fixed):
const newStartDate = getLocalDateString(event.start);
let newEndDate = newStartDate;
if (event.end) {
    const endDate = new Date(event.end);
    endDate.setDate(endDate.getDate() - 1);
    newEndDate = getLocalDateString(endDate);
}

Task Creation:

// Before (buggy):
startDate: dates[0].toISOString().split('T')[0],
endDate: (dates[1] || dates[0]).toISOString().split('T')[0],

// After (fixed):
startDate: getLocalDateString(dates[0]),
endDate: getLocalDateString(dates[1] || dates[0]),

Calendar Rendering:

// Before (buggy):
const exclusiveEnd = new Date(task.endDate || task.startDate);
exclusiveEnd.setDate(exclusiveEnd.getDate() + 1);
end: exclusiveEnd.toISOString().split('T')[0],

// After (fixed):
const startDate = createLocalDate(task.startDate);
const endDate = createLocalDate(task.endDate || task.startDate);
const exclusiveEndDate = new Date(endDate);
exclusiveEndDate.setDate(exclusiveEndDate.getDate() + 1);
end: getLocalDateString(exclusiveEndDate),

Testing

Created comprehensive tests covering:

  • ✅ Date string consistency across timezone conversions
  • ✅ Calendar drag & drop date preservation
  • ✅ Task creation date accuracy
  • ✅ Date round-trip consistency
  • ✅ Compatibility with existing task data

All tests pass, confirming the fixes work correctly across different timezones without breaking existing functionality.

Impact

  • Calendar drag & drop: Now preserves exact intended dates
  • Task creation: User-selected dates are saved correctly
  • Existing tasks: Remain unaffected by the changes
  • Cross-timezone: Works consistently regardless of user timezone

Fixes #[issue-number] (カレンダーの日付オフセットバグ修正)


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Jul 1, 2025

Copy link
Copy Markdown

Deploying pliny-v2 with  Cloudflare Pages  Cloudflare Pages

Latest commit: 2b3d59a
Status: ✅  Deploy successful!
Preview URL: https://dc229ffc.pliny-v2.pages.dev
Branch Preview URL: https://copilot-fix-9ee53632-be58-46.pliny-v2.pages.dev

View logs

@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Jul 1, 2025

Copy link
Copy Markdown

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Updated (UTC)
❌ Deployment failed
View logs
pliny-v2 2b3d59a Jul 01 2025, 03:59 AM

Co-authored-by: bata-san <140896603+bata-san@users.noreply.github.com>
Copilot AI changed the title [WIP] カレンダーの日付オフセットバグを修正 Fix date offset bugs in calendar drag&drop and task creation Jul 1, 2025
Copilot AI requested a review from bata-san July 1, 2025 04:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants