How to Build a Booking Time-Slot Demo in Plain JavaScript

5 min read By Simple Tech Path

The short answer

A booking demo needs three things: a list of services with how long they take, the opening hours and a list of bookings already made. For each start time in the day, check whether the service would fit before closing time and would not overlap an existing booking. The ones that pass are the free slots.

I built one for a fictional car wash, in about 130 lines of plain JavaScript and no libraries. This article shows the core of it, and also what it can not do.

Booking demo for a fictional car wash showing available time slots, with the already booked ones crossed out

The data

Everything is described in one small object:

var CONFIG = {
  open: '08:00',
  close: '18:00',
  step: 30,                       // a new slot every 30 minutes
  openDays: [1, 2, 3, 4, 5, 6],   // Monday to Saturday
  services: [
    { id: 'express', name: 'Express wash (30 min)', minutes: 30 },
    { id: 'full',    name: 'Full wash (60 min)',    minutes: 60 },
    { id: 'detail',  name: 'Detail package (120 min)', minutes: 120 }
  ]
};

Keeping the hours and services in one place means a new business only needs a new config, not new logic.

Working with times as minutes

Comparing “13:30” and “09:00” as text is awkward. It is easier to convert times to minutes since midnight:

function toMin(hhmm) {
  var p = hhmm.split(':');
  return +p[0] * 60 + +p[1];
}

Now “09:00” is 540 and “10:00” is 600, and every comparison is plain arithmetic.

The overlap check

Two bookings overlap when each one starts before the other ends. That is the only rule you need:

function overlaps(list, date, start, end) {
  return list.some(function (b) {
    return b.date === date && toMin(b.start) < end && start < toMin(b.end);
  });
}

If a 60-minute wash is booked from 09:00 to 10:00, a new booking at 09:30 overlaps it, and one at 10:00 does not.

Generating the slots

Loop through the day, step by step, and stop when the service would run past closing time:

for (var t = toMin(CONFIG.open); t + minutes <= toMin(CONFIG.close); t += CONFIG.step) {
  var taken = overlaps(list, date, t, t + minutes);
  // draw a button; disable it if taken
}

Because the loop uses the service length, a 120-minute detail package gets fewer start times than a 30-minute wash, and it never offers a time that would end after closing.

I tested the demo with a Full wash on a Tuesday and two sample bookings at 09:00 and 13:00. Out of 19 possible start times, exactly six were marked as taken: 08:30, 09:00 and 09:30 around the first booking, and 12:30, 13:00 and 13:30 around the second. That is what the logic predicts, since a 60-minute wash starting 30 minutes before a booking would run into it.

A date bug worth knowing

The first version of this kind of code often uses new Date().toISOString() to get “today”. That returns the date in UTC. In a time zone behind UTC, in the evening it already shows tomorrow’s date, and the picker would block the wrong day.

Build the date from the local parts instead:

function localISO(d) {
  return d.getFullYear() + '-' +
    String(d.getMonth() + 1).padStart(2, '0') + '-' +
    String(d.getDate()).padStart(2, '0');
}

Saving the bookings

In the demo, bookings are saved with local storage:

localStorage.setItem('demo_bookings', JSON.stringify(list));

This is exactly where the demo stops being a real system. The data stays in that visitor’s browser. The owner cannot see it, other devices do not have it and clearing the browser erases it. Two customers can book the same slot because neither knows about the other.

A browser-only demo compared with a real system that uses a server and a database

From demo to real system

To make it real, the same logic would run on a server, and the bookings would be saved in a shared database. The server would also repeat the overlap check at the moment of saving, so two people cannot take the same slot even if they click at the same time. I describe the other parts, such as accounts and payments, in my article on what a custom system really includes.

Sending the confirmation

The demo ends with a link that opens WhatsApp with a ready message:

'https://wa.me/' + number + '?text=' + encodeURIComponent(text)

The customer taps it and sends the message, so the owner receives the details in a chat they already use. It is a simple way to close the loop while the booking itself stays in the browser.

How to test it

Before you show a booking page to anyone, try these cases by hand:

  1. Pick a Sunday. The page should say the business is closed, not offer slots.
  2. Book a slot, then pick the same service and date again. The slot you booked should now be crossed out, and so should the ones just before it, because they would run into it.
  3. Switch to the longest service. The last start times of the day should disappear, because the job would end after closing.
  4. Change the date and come back. The taken slots should still be marked.
  5. Reload the page. The bookings should still be listed, because local storage survives a reload, but check again in a private window to see them disappear.

That last test is a useful reminder of what the demo is. If the bookings vanish in a private window, the owner could never rely on them.

Adding a lunch break

A break is just another range that blocks slots. You can add a list of blocked ranges to the config and treat them like bookings:

var BREAKS = [{ start: '12:00', end: '13:00' }];

Then, inside the loop, also skip a start time when it overlaps a break, using the same arithmetic as the overlap check. Days off, such as holidays, work the same way with a list of dates.

Frequently asked questions

Can I use this code for a real business? Use it to learn and to show a flow. For a real business, move the logic to a server with a database.

How do I change the step between slots? Change step in the config. A smaller step gives more start times.

What about breaks, such as lunch? Add them as blocked ranges and check them the same way as bookings.

Does it work on a phone? The layout adapts to narrow screens. I checked it at a 500-pixel-wide view, which is a narrow phone-like width, and the time slots rearranged into four columns. I did not test it on a physical phone.

#astro

How I Built a Blog with Astro and Claude Code

How I built a fast static blog with the free Astro template, Claude Code and a GitHub build check, plus the draft-post bug I found along the way.

5 min read