You have a working schedule logic that filters out the timestamps already passed if the start date is today, but when you pick a future start date (like 1st July from June 26th), currently it just takes all times as-is (which is correct), but in your use-case, you want a slightly different behavior — you want it to pick the upcoming timestamp relative to now when the start date is today, but when start date is a future date, you want all times included normally without skipping or filtering and without skipping corresponding videos.
From your description, you mention that when starting from 1 July (a future date), some timestamps and videos are skipped wrongly — likely because your loop logic still assumes some skipping/filtering cause.
Problem Summary
- For today: Skip timestamps already passed (e.g., if 7pm now, 4am and 6am are skipped).
- For future dates: Use all timestamps as is for scheduling from start day with videos assigned in order.
- Bug: Starting schedule from a future date skips timestamps and videos unexpectedly. Maybe the filtering or the baseDayOffset logic or queue resetting causes skipping.
Solution
You already use this check in your code:
let scheduleQueue;
if (startDate.toDateString() === now.toDateString()) {
scheduleQueue = selectedTimes.filter(t => {
const [h, m] = t.split(':');
const temp = new Date();
temp.setHours(parseInt(h), parseInt(m), 0, 0);
return temp > now;
});
} else {
scheduleQueue = [...selectedTimes];
}
This is correct: for future days, all times included.
However, the problem likely happens in the loop for assigning scheduled times:
for (let i = 0; i < files.length; i++) {
if (scheduleQueue.length === 0) {
baseDayOffset += (1 + skipDays);
scheduleQueue = [...selectedTimes];
}
const scheduledTimeStr = scheduleQueue.shift();
// ...
}
This means for every video you assign a time from scheduleQueue, and when queue empty, you move to next day and refill scheduleQueue.
Hypothesis: Where is skipping happening?
If scheduleQueue is initialized differently or emptied because some times are filtered out at start, the loop skips videos as well while scheduleQueue.shift() is the scheduling time.
If start date is in future, that should not happen: scheduleQueue includes all times, so it should cover all videos.
Possible issue:
- The
scheduleQueue.filter() for "today" is time-sensitive.
- When schedule start date != today, you keep all times — but you are doing no more filtering inside the loop (which is good).
- For today, you filter timestamps before assignment.
- But the problem is that for today, when filtered times are less than videos, the day offset increases and scheduleQueue resets.
- But for a future day, if you did not fix that logic, it may skip as well unclear re-initializations.
Recommended fix:
-
Separate "today" logic from "future day" logic more explicitly.
-
On first day (startDate) assign only the appropriate timestamps (filtered or not).
-
For subsequent days (startDate + baseDayOffset > startDate), always assign all timestamps (no filtering).
To do so:
-
Keep a flag to detect if it's first day being scheduled.
-
When baseDayOffset == 0 (start date), use filtered times if today or all times if future date.
-
When baseDayOffset > 0 (next scheduled days), always use all times.
Incorporating this logic:
Replace:
if (scheduleQueue.length === 0) {
// Move forward skipDays + 1 days
baseDayOffset += (1 + skipDays);
// Reset scheduleQueue with full times on new day -- no filtering needed for future days
scheduleQueue = [...selectedTimes];
}
With something like:
for (let i = 0; i < files.length; i++) {
if (scheduleQueue.length === 0) {
baseDayOffset += (1 + skipDays);
// For subsequent days, always use all times (no filter)
scheduleQueue = [...selectedTimes];
}
const scheduledTimeStr = scheduleQueue.shift();
// ...
}
But also initialize scheduleQueue for day 0 carefully:
let scheduleQueue;
if (startDate.toDateString() === now.toDateString()) {
// Today: only upcoming times
scheduleQueue = selectedTimes.filter(t => {
const [h, m] = t.split(':');
const temp = new Date();
temp.setHours(parseInt(h), parseInt(m), 0, 0);
return temp > now;
});
} else {
// Future start date: all times in first queue
scheduleQueue = [...selectedTimes];
}
if (scheduleQueue.length === 0) {
alert("No upload times remain on the start scheduling day after filtering out past times.");
return;
}
That is how you already do it.
Final tweak to avoid skipping videos:
The problem you saw is videos are skipped because at the start you filtered out time slots, so for example on 26th June at 7pm, only 9pm remains (2 timestamps skipped), so only one video scheduled on 26th June.
If user selects start date 1 July (future), you want to keep all (no filtering).
Perfect.
If your loop works on this, all videos will be scheduled correctly.
If this is still causing issues, enhance by:
- Using a helper function to get times for a specific scheduled date:
function getTimesForDate(date) {
if (date.toDateString() === now.toDateString()) {
// Only upcoming times
return selectedTimes.filter(t => {
const [h, m] = t.split(':');
const temp = new Date(date);
temp.setHours(parseInt(h), parseInt(m), 0, 0);
return temp > now;
});
} else {
return [...selectedTimes];
}
}
- During iterations, when
scheduleQueue is empty (moving to next scheduled date):
baseDayOffset += (1 + skipDays);
const nextDate = new Date(startDate);
nextDate.setDate(nextDate.getDate() + baseDayOffset);
scheduleQueue = getTimesForDate(nextDate);
if (scheduleQueue.length === 0) {
alert(`No upload times remain on the day ${nextDate.toDateString()} after filtering.`);
return;
}
- This prevents skipping times on new days if the "now" day logic is always applied only for the current actual date (now).
Full modified section in your submit listener:
document.getElementById('uploadForm').addEventListener('submit', async function(event) {
event.preventDefault();
// ... your existing code ...
const startDate = new Date(startDateInput + "T00:00:00");
const now = new Date();
function getTimesForDate(date) {
if (date.toDateString() === now.toDateString()) {
return selectedTimes.filter(t => {
const [h, m] = t.split(':');
const temp = new Date(date);
temp.setHours(parseInt(h), parseInt(m), 0, 0);
return temp > now;
});
} else {
return [...selectedTimes];
}
}
let baseDayOffset = 0;
let scheduleQueue = getTimesForDate(startDate);
if (scheduleQueue.length === 0) {
alert(`No upload times remain on the start scheduling day (${startDate.toDateString()}) after filtering out past times.`);
return;
}
// ... your progress display code ...
for (let i = 0; i < files.length; i++) {
if (scheduleQueue.length === 0) {
baseDayOffset += (1 + skipDays);
const nextDate = new Date(startDate);
nextDate.setDate(startDate.getDate() + baseDayOffset);
scheduleQueue = getTimesForDate(nextDate);
if (scheduleQueue.length === 0) {
alert(`No upload times remain on scheduled day (${nextDate.toDateString()}) after filtering.`);
return;
}
}
const scheduledTimeStr = scheduleQueue.shift();
const [hh, mm] = scheduledTimeStr.split(':');
const scheduledDate = new Date(startDate);
scheduledDate.setDate(startDate.getDate() + baseDayOffset);
scheduledDate.setHours(parseInt(hh), parseInt(mm), 0, 0);
// ... rest of your upload logic ...
}
// ... rest of the code ...
});
Summary
-
Only on the actual "today" date (now) do you filter out past times.
-
For any other date, keep all timestamps.
-
Change your logic inside the loop to respect that by calling getTimesForDate(date) for each new scheduled day, so correct filtering is applied.
-
This prevents skipping videos or timestamps incorrectly when scheduling from a future start date.
If you want, I can provide you with the full updated <script> submit listener that implements this fix. Just ask!