Skip to main content

DHTMLX Booking. Time format example

Live example​

Slot times are printed by the locale, so the clock format is one string inside it. formats.timeFormat takes a pattern: "%g:%i %a" prints 1:30 pm and "%H:%i" prints 13:30. This demo rewrites that string on the built-in en locale and applies it with setLocale, wired here to a button.

let clockFormat = 12;
const locale = booking.locales["en"];
setTimeFormat(clockFormat);

function setTimeFormat(clock){
    locale.formats.timeFormat = clock === 12 ? "%g:%i %a" : "%H:%i";
}

const widget = new booking.Booking("#root", {
	start: buildToday(),
    locale, 
    data: dataset,
});


setLabel(clockFormat);

function confirmCallback(ev){
    const confirm = ev.confirm;
    setTimeout(() => {
        Math.random() < 0.5 ? confirm.error() : confirm.done();
    }, 1000);
}

widget.setConfirmHandler(confirmCallback);

function changeFormat() {
    clockFormat = clockFormat === 12 ? 24  : 12;
    setTimeFormat(clockFormat);
    widget.setLocale(locale);
    widget.setConfirmHandler(confirmCallback);

    setLabel(clockFormat);
}

function setLabel(value){
    document.querySelector("#label").innerHTML = `${value}-hour clock format`;
}
<!-- custom styles -->
<link rel="stylesheet" href="https://snippet.dhtmlx.com/codebase/assets/css/auxiliary_controls.css">

<div class="g-wrap" style="height: 100%;">
    <!-- Locale selector -->

    <div class="dhx_sample-controls">
        <span class="dhx_sample-label" id="label"></span>
        <button class="dhx_sample-btn dhx_sample-btn--cta" id="change_locale" onclick="changeFormat()">Change</button>
    </div>

    <!-- component container -->
    <div id="root" style="height:calc(100% - 60px)"></div>
</div>
<!-- dataset -->

<script>
function buildToday(){const params = new URLSearchParams(typeof location !== 'undefined' ? location.search : '');const fixed = params.get('fixedDate') || (typeof window !== 'undefined' && window.FIXED_DATE);return fixed ? new Date(fixed) : new Date();}

	const dataset = [
      {
        id: "ee828b5d-a034-420c-889b-978840015d6a",
        title: "Natalie Tyson",
        category: "Therapist",
        subtitle: "2 years of experience",
        details: "Cleveland Clinic\n9500 Euclid Ave",
        preview: "https://snippet.dhtmlx.com/codebase/data/booking/01/img/01.jpg",
        price: "$35",
        review: {
            stars: 4,
            count: 120,
        },
        slots: [
            {
                from: 9,
                to: 20,
                days: [1, 2, 3, 4, 5],
            },
            {
                from: 10,
                to: 18,
                days: [6, 0],
            },
        ],
    },
      {
        id: "5c9b64ad-1830-4e5b-a5f9-8acea10706df",
        title: "James Anderson",
        category: "Allergist",
        subtitle: "3 years of experience",
        details: "UCLA Medical Center\n57 Westwood Plaza",
        preview: "https://snippet.dhtmlx.com/codebase/data/booking/01/img/11.jpg",
        price: "$30",
        review: {
            stars: 4,
            count: 64,
        },
        slotSize: 45,
        slotGap: 10,
        slots: [
            {
                from: "9:15",
                to: 17,
                days: [1, 2, 3, 4, 5],
            },
        ],
    },
      // ... 44 more items (see Live Editor for full data)
    ];

function getDate(addDays, hoursValue = 0, minutesValue = 0) {
	const date = buildToday();
	const secondsValue = 0; // round to minutes
	const msValue = 0;

	date.setDate(date.getDate() + addDays);
	date.setHours(hoursValue, minutesValue, secondsValue, msValue);

	return date.getTime();
}
</script>

Use case​

Clock format is a habit, and it splits by country rather than by language. A booking page for a US clinic is read as 9:30 am, and the same page for a European office is read as 09:30. An internal tool used by both offices follows the account rather than the build. Because the format lives in the locale, the app can pick it from a user profile at load time and apply another pattern later with setLocale. The pattern is a string, so a business that prints 9:30 AM in every other screen can match it here with %A.

How to do this in your app​

  • Copy the English locale: spread booking.locales["en"] and give the copy a new formats object. const locale = { ...booking.locales["en"], formats: { ...booking.locales["en"].formats } }; The built-in formats object is shared with the default English locale, so the pattern belongs on the copy.
  • Write the pattern: locale.formats.timeFormat = "%g:%i %a" for a 12 hour clock, or "%H:%i" for a 24 hour one.
  • Apply it at start: pass the object to the constructor as locale.
  • Switch it later: change the string and call widget.setLocale(locale). The call destroys the current widget and initializes a new one, and it resets handlers attached with setConfirmHandler or api.on. Set those handlers again after the call. The open day, the selected slot, and the search text are cleared. The demo puts that behind a button, and any control or code path can call it.

Good to know​

  • The pattern characters: %g gives hours from 1 to 12 and %h adds the leading zero, %G gives hours from 0 to 23 and %H adds the zero, %i gives minutes, %a prints am or pm, and %A prints AM or PM.
  • Dates use a separate key: formats.dateFormat prints the date in the filter. English ships with "%m/%d/%Y", German with "%d.%m.%Y", and Chinese with "%Y-%m-%d". Slot day headers use booking.monthDayFormat, which is "%M %d" in all three locales.
  • A built-in locale is a shared object: this demo edits booking.locales["en"] in place. A copy for your app has to include a new formats object, as in the steps above. Spreading only the locale keeps the same formats object, and an edit there changes the default English clock. setLocale() with no argument then returns that edited pattern.

FAQ​

How do you display a 12 hour clock in a booking calendar?​

Set formats.timeFormat to "%g:%i %a" on the locale you pass to the widget. Use "%h:%i %A" when the hour needs a leading zero and the marker has to be uppercase.

How do you change the time format after the widget is created?​

Edit formats.timeFormat on a copy of the locale and call setLocale() with that object. setLocale() with no argument, or setLocale(null), returns the default English locale. An edit of booking.locales["en"].formats writes the pattern onto that default, so the reset keeps the edited clock.

Does the time format change the dates as well?​

No. The filter date follows formats.dateFormat. The slot day headers follow booking.monthDayFormat. formats.timeFormat is a separate key.

API reference​

Additional resources​