Date picker set from 'Tomorrow' only. Working in preview but not when published. Help please!

I’m having trouble with Date picker working as it should in preview mode but not when published.

I have set the Velo code to allow dates from ‘Tomorrow’ only which is working fine when tested in preview mode. But when published, today is still an option. I’m at a bit of a loss. Anything I should check?

Working in
Working in Wix editor. Using Velo AI code in Studio to help with coding

Site link
Book Your Spot for Wood fired sourdough pizza in the Cotswolds | Fire & Wine

What I’m trying to do
Restrict the bookings to from tomorrow only. And only on a Thursday to Sunday

Mindate set to ‘Tomorrow’

Programming for Thursday to Sunday, and with corresponding available times all working as it should

Working in preview but not when published. Not sure why this is the case

What I’ve tried so far
Setting the date from ‘Tomorrow’ using Velo code. This is working in ‘Preview’ mode but not when published

Can you post the code so we can see if it is something in there not firing correctly.

Thanks @Dan_Suhr

$w.onReady(function () {

updateTimeDropdown();

restrictDatePicker();

});

function updateTimeDropdown() {

**const** dropdown = $w('#timedropdown');

**const** now = **new** Date();

**const** day = now.getDay(); // 0 = Sunday, 4 = Thursday, 5 = Friday, 6 = Saturday

**let** startHour = 17; // 5:00 PM

**let** endHour;



**if** (day === 4 || day === 0) { // Thursday or Sunday

    endHour = 20; // 8:00 PM

} **else** **if** (day === 5 || day === 6) { // Friday or Saturday

    endHour = 21; // 9:00 PM

} **else** {

    // Not a valid day for time slots

    dropdown.options = \[{ label: 'No available slots today', value: '' }\];

    dropdown.value = '';

    **return**;

}



// Build time slots in 15-minute intervals

**let** options = \[\];

**for** (**let** hour = startHour; hour < endHour; hour++) {

    **for** (**let** min = 0; min < 60; min += 15) {

        **const** slotDate = **new** Date(now.getFullYear(), now.getMonth(), now.getDate(), hour, min, 0, 0);

        **const** label = formatTime(slotDate);

        **const** value = slotDate.toISOString();

        options.push({ label, value });

    }

}

// Add the last slot at endHour:00

**const** lastSlot = **new** Date(now.getFullYear(), now.getMonth(), now.getDate(), endHour, 0, 0, 0);

**if** (lastSlot > now) {

    options.push({ label: formatTime(lastSlot), value: lastSlot.toISOString() });

}



**if** (options.length === 0) {

    dropdown.options = \[{ label: 'No available slots today', value: '' }\];

    dropdown.value = '';

} **else** {

    dropdown.options = options;

    dropdown.value = options\[0\].value;

}

}

function restrictDatePicker() {

**const** datePicker = $w('#datePicker2');

**const** tomorrow = **new** Date();

tomorrow.setDate(tomorrow.getDate() + 1);

tomorrow.setHours(0, 0, 0, 0); // Set time to midnight



datePicker.minDate = tomorrow;

}

function formatTime(date) {

**let** hours = date.getHours();

**let** minutes = date.getMinutes();

**let** ampm = hours >= 12 ? 'PM' : 'AM';

hours = hours % 12;

hours = hours ? hours : 12; // the hour '0' should be '12'

**let** minutesStr = minutes < 10 ? '0' + minutes : minutes;

**return** hours + ':' + minutesStr + ' ' + ampm;

}

I think you might be running into this issue - Time Zone | Velo

It might be that the date picker timezone is by default a different timezone to yours - if you explicitly set it, does the issue persist?

The timezone suggestion from Noah is a good one, but I think there is also another issue in the code.
I think your updateTimeDropdown() function is using today’s date:

const now = new Date();
const day = now.getDay();

So even though the date picker is being restricted to tomorrow, the time dropdown is still being built based on the current day, not the selected booking date.

I would try setting the minDate first, then updating the time dropdown based on the selected date.
something like this

function restrictDatePicker() {
    const datePicker = $w('#datePicker2');

    const tomorrow = new Date();
    tomorrow.setDate(tomorrow.getDate() + 1);
    tomorrow.setHours(0, 0, 0, 0);

    datePicker.minDate = tomorrow;
    datePicker.value = tomorrow;
}

Also make sure the site timezone is set correctly in the Wix dashboard. Preview and live can behave differently if the site/browser timezone handling is not lining up.

Thanks for the suggestion. I’ve updated this but still having the same issue… One down

Thank you for the help. I’ve checked the site time zone and all ok. Tried adding +1 to const day in timepicker but am still able to choose today. Thinking changing the time zone of the site may be an easier solve for this?..

Still no joy sadly - updated current code…

$w.onReady(function () {
    updateTimeDropdown();
    restrictDatePicker();
});

function updateTimeDropdown() {
    const dropdown = $w('#timedropdown');
    const now = new Date() + 1;
    const day = now.getDay(); // 0 = Sunday, 4 = Thursday, 5 = Friday, 6 = Saturday
    let startHour = 17; // 5:00 PM
    let endHour;

    if (day === 4 || day === 0) { // Thursday or Sunday
        endHour = 20; // 8:00 PM
    } else if (day === 5 || day === 6) { // Friday or Saturday
        endHour = 21; // 9:00 PM
    } else {
        // Not a valid day for time slots
        dropdown.options = [{ label: 'No available slots today', value: '' }];
        dropdown.value = '';
        return;
    }

    // Build time slots in 15-minute intervals
    let options = [];
    for (let hour = startHour; hour < endHour; hour++) {
        for (let min = 0; min < 60; min += 15) {
            const slotDate = new Date(now.getFullYear(), now.getMonth(), now.getDate(), hour, min, 0, 0);
            const label = formatTime(slotDate);
            const value = slotDate.toISOString();
            options.push({ label, value });
        }
    }
    // Add the last slot at endHour:00
    const lastSlot = new Date(now.getFullYear(), now.getMonth(), now.getDate(), endHour, 0, 0, 0);
    if (lastSlot > now) {
        options.push({ label: formatTime(lastSlot), value: lastSlot.toISOString() });
    }

    if (options.length === 0) {
        dropdown.options = [{ label: 'No available slots today', value: '' }];
        dropdown.value = '';
    } else {
        dropdown.options = options;
        dropdown.value = options[0].value;
    }
}

// Get the date picker timezone
let timeZone = $w("#datePicker2").timeZone; // "Europe/London"

function restrictDatePicker() {
    const datePicker = $w('#datePicker2');
    const tomorrow = new Date();
    tomorrow.setDate(tomorrow.getDate() + 1);
    tomorrow.setHours(0, 0, 0, 0); //set time to midnight
    datePicker.minDate = tomorrow;
    datePicker.value = tomorrow;
}

function formatTime(date) {
    let hours = date.getHours();
    let minutes = date.getMinutes();
    let ampm = hours >= 12 ? 'PM' : 'AM';
    hours = hours % 12;
    hours = hours ? hours : 12; // the hour '0' should be '12'
    let minutesStr = minutes < 10 ? '0' + minutes : minutes;
    return hours + ':' + minutesStr + ' ' + ampm;
}

Okay, I think I’ve got a working version that meets all the criteria outlined in the conversation :slight_smile:

It was a little easier to refactor some of the code and use some of the native properties that the Date Picker supports.


Demo: https://noahlwix.wixstudio.com/date-ranges


I’ve simplified some of code to make it more robust. Here’s what changed:

1. Using Native Date Picker Properties

  • Moved away from manual day-of-week validation in JavaScript
  • Now uses the native disabledDaysOfWeek property to disable Mon–Wed directly in the UI
  • Users can’t even click on unavailable days, which is probably a better UX

2. Smart Date Selection

  • The code now automatically finds the first available date starting from tomorrow
  • If tomorrow isn’t a valid day (e.g., it’s a Monday), it will skip ahead to the next valid day (Thursday in this case)
  • Example: If today is Sunday, tomorrow (Monday) is skipped, and Thursday is selected as the default instead

3. Removed Complexity

  • Eliminated redundant validation logic
  • Removed the unused timeZone variable
  • Cleaner date handling and formatting

4. Dynamic Time Slot Updates

  • Added an onChange listener to the date picker
  • When users select a different date, the time dropdown automatically updates to reflect the correct hours for that day
  • Thu/Sun: 5–8 PM | Fri/Sat: 5–9 PM

What Gets Disabled

Day Available?
Monday :cross_mark: Disabled
Tuesday :cross_mark: Disabled
Wednesday :cross_mark: Disabled
Thursday :white_check_mark: 5–8 PM
Friday :white_check_mark: 5–9 PM
Saturday :white_check_mark: 5–9 PM
Sunday :white_check_mark: 5–8 PM

The minDate is always set to tomorrow, so today can never be selected even if it’s a valid day.


Docs used:


The code

$w.onReady(function () {
    restrictDatePicker();
    updateTimeDropdown();

    // Update time dropdown when date picker changes
    $w('#datePicker2').onChange(() => updateTimeDropdown());
});

function restrictDatePicker() {
    const datePicker = $w('#datePicker2');
    const validDays = [0, 4, 5, 6]; // Thursday (4), Friday (5), Saturday (6), Sunday (0)

    // Set minDate to tomorrow
    const tomorrow = new Date();
    tomorrow.setDate(tomorrow.getDate() + 1);
    tomorrow.setHours(0, 0, 0, 0);
    datePicker.minDate = tomorrow;

    // Find the first available booking date (starting from tomorrow)
    let firstAvailableDate = new Date(tomorrow);
    while (!validDays.includes(firstAvailableDate.getDay())) {
        firstAvailableDate.setDate(firstAvailableDate.getDate() + 1);
    }
    datePicker.value = firstAvailableDate;

    // Disable Monday (1), Tuesday (2), Wednesday (3)
    // Allow only Thursday (4), Friday (5), Saturday (6), Sunday (0)
    datePicker.disabledDaysOfWeek = [1, 2, 3];
}

function updateTimeDropdown() {
    const dropdown = $w('#timedropdown');
    const selectedDate = $w('#datePicker2').value;

    if (!selectedDate) {
        dropdown.options = [{ label: 'Select a date first', value: '' }];
        return;
    }

    const day = selectedDate.getDay(); // 0 = Sunday, 4 = Thursday, 5 = Friday, 6 = Saturday
    const startHour = 17; // 5:00 PM
    let endHour = day === 4 || day === 0 ? 20 : 21; // Thu/Sun: 8 PM, Fri/Sat: 9 PM

    // Build time slots in 15-minute intervals
    let options = [];
    for (let hour = startHour; hour < endHour; hour++) {
        for (let min = 0; min < 60; min += 15) {
            const slotDate = new Date(selectedDate);
            slotDate.setHours(hour, min, 0, 0);
            options.push({
                label: formatTime(slotDate),
                value: slotDate.toISOString()
            });
        }
    }

    // Add final slot at endHour:00
    const lastSlot = new Date(selectedDate);
    lastSlot.setHours(endHour, 0, 0, 0);
    options.push({
        label: formatTime(lastSlot),
        value: lastSlot.toISOString()
    });

    dropdown.options = options.length > 0 ? options : [{ label: 'No available slots', value: '' }];
    if (options.length > 0) {
        dropdown.value = options[0].value;
    }
}

function formatTime(date) {
    let hours = date.getHours();
    let minutes = date.getMinutes();
    let ampm = hours >= 12 ? 'PM' : 'AM';
    hours = hours % 12 || 12; // Convert 0 to 12
    let minutesStr = minutes < 10 ? '0' + minutes : minutes;
    return `${hours}:${minutesStr} ${ampm}`;
}

Thanks so much for your help. I’ve cleared all previous code and added in the above. On the test site it’s very much working but when put live I unfortunately still have the same issue. Just giving it some time to see if it sorts…

This has now worked! Thank you so much. I can’t tell you how many sleepless nights I’ve had around this – Book Your Spot for Wood fired sourdough pizza in the Cotswolds | Fire & Wine

Glad we could help! :handshake:

Sorry, I’ve found one little issue with the time picker that I can’t seem to solve. Rather than showing only times from 5pm to 8pm on Thursdays and Sundays, and 5pm to 9pm on Fridays and Saturdays it’s showing 15 minute slots available all day. I can see the code has set the intervals and the time restrictions but this doesn’t seem to be restricting the time slots available by day

I’ve just reverted the code to 8pm finish to avoid any customer issues short term.

Did you solve this? The times appear to be showing correctly, and the date picker is working as I would expect

Thanks for replying Noah. No, sadly I’m still having the same issue. I’ve just reverted the code so that you can see the times are showing from midnight to midnight in 15 minute slots. I have just republished. It’s allowing times after 8pm on Thursday and Sundays

That’s odd! It’s showing correctly for me :upside_down_face:


Perhaps more explicit code for the updateTimeDropdown would help.

Something like:

function updateTimeDropdown() {
    const dropdown = $w('#timedropdown');
    const selectedDate = $w('#datePicker2').value;

    dropdown.options = [];

    if (!selectedDate) {
        dropdown.options = [{ label: 'Select a date first', value: '' }];
        return;
    }

    const day = selectedDate.getDay();
    const startHour = 17; // 5:00 PM

    // Explicitly define endHour
    let endHour;
    if (day === 0 || day === 4) { // Sunday or Thursday
        endHour = 20; // 8 PM
    } else {
        endHour = 21; // 9 PM
    }

    let options = [];
    for (let hour = startHour; hour < endHour; hour++) {
        for (let min = 0; min < 60; min += 15) {
            let slot = new Date(selectedDate);
            slot.setHours(hour, min, 0, 0);
            options.push({
                label: formatTime(slot),
                value: slot.toISOString()
            });
        }
    }

    let finalSlot = new Date(selectedDate);
    finalSlot.setHours(endHour, 0, 0, 0);
    options.push({
        label: formatTime(finalSlot),
        value: finalSlot.toISOString()
    });

    dropdown.options = options;
    dropdown.value = options[0].value;
}