Agent-readable demo data
- Title
- Hiding Non-Business Hours (JavaScript Event Calendar) | DayPilot Pro for JavaScript Demo
- Tree
- DayPilot JavaScript Demo
- Catalog root
- Default
- Description
- Room bookings. Business hours: 08:00–18:00.
Select a time to book a room; drag or resize to change it.
Documentation.
Inline demo script 1
const app = {
eventsByPeriod: new Map(),
elements: {
status: document.querySelector("#status"),
period: document.querySelector("#period"),
previous: document.querySelector("#previous"),
next: document.querySelector("#next"),
showNonBusiness: document.querySelector("#show-nonbusiness"),
hideWeekends: document.querySelector("#hide-weekends"),
skipNonBusiness: document.querySelector("#skip-nonbusiness")
},
init() {
this.showPeriod();
this.addEventHandlers();
},
addEventHandlers() {
this.elements.showNonBusiness.addEventListener("change", () => {
this.applyFilters();
});
this.elements.hideWeekends.addEventListener("change", () => {
this.applyFilters();
});
this.elements.skipNonBusiness.addEventListener("change", ev => {
dp.update({ eventMoveSkipNonBusiness: ev.target.checked });
});
this.elements.previous.addEventListener("click", () => {
this.navigate(-1);
});
this.elements.next.addEventListener("click", () => {
this.navigate(1);
});
},
applyFilters() {
dp.clearSelection();
dp.update(this.filter());
},
colors: {
planning: { background: "#edf3fc", border: "#c9d8ed", text: "#294b76", accent: "#5b83bb" },
design: { background: "#edf7f3", border: "#c5dfd4", text: "#28594c", accent: "#4f9680" },
research: { background: "#f4f0fa", border: "#dcd0eb", text: "#594171", accent: "#9472b7" },
delivery: { background: "#fcf5e8", border: "#e9d7b1", text: "#71542a", accent: "#bd944f" },
support: { background: "#f0f3f6", border: "#d4dce4", text: "#49596c", accent: "#8394a7" }
},
eventDetails(data) {
const start = new DayPilot.Date(data.start);
const end = new DayPilot.Date(data.end);
const compact = end.ticks - start.ticks <= 3600000;
const sameDay = start.getDatePart().ticks === end.getDatePart().ticks;
const endFormat = sameDay ? "H:mm" : "ddd H:mm";
const label = start.toString("ddd H:mm") + " – " + end.toString(endFormat);
return { compact, label };
},
renderEvent(args) {
const data = args.data;
const color = this.colors[data.tags?.category] || this.colors.planning;
const details = this.eventDetails(data);
data.cssClass = "booking-card" + (details.compact ? " booking-card-compact" : "");
data.backColor = color.background;
data.borderColor = color.border;
data.fontColor = color.text;
data.barColor = color.accent;
data.barBackColor = color.background;
data.toolTip = data.text + "\n" + details.label;
let html = '<div class="booking-content">';
html += '<div class="booking-title">' + DayPilot.Util.escapeHtml(data.text) + '</div>';
if (!details.compact) {
html += '<div class="booking-meta">' + DayPilot.Util.escapeHtml(details.label) + '</div>';
}
data.html = html + '</div>';
},
exportEvent(args) {
const data = args.e.data;
const details = this.eventDetails(data);
const color = this.colors[data.tags?.category] || this.colors.planning;
args.text = data.text + (details.compact ? "" : "\n" + details.label);
args.fontColor = color.text;
args.fontSize = 12;
},
week: DayPilot.Date.today().firstDayOfWeek(1),
filter() {
const showHours = this.elements.showNonBusiness.checked;
const hideWeekends = this.elements.hideWeekends.checked;
return {
showNonBusiness: showHours,
businessWeekends: !hideWeekends,
onIncludeTimeCell: showHours && hideWeekends ? args => {
const day = args.cell.start.dayOfWeek();
args.cell.visible = day !== 0 && day !== 6;
} : null
};
},
events() {
return [
{ id: "handover", resource: "A", text: "Design workshop", start: this.week.addHours(16), end: this.week.addDays(1).addHours(10), tags: { category: "design" } },
{ id: "review", resource: "B", text: "Review", start: this.week.addDays(1).addHours(10), end: this.week.addDays(1).addHours(12), tags: { category: "design" } },
{ id: "night", moveSkipNonBusinessDisabled: true, resource: "C", text: "Night maintenance", start: this.week.addHours(22), end: this.week.addDays(1).addHours(2), tags: { category: "support" } },
{ id: "closing", resource: "B", text: "Client workshop", start: this.week.addDays(1).addHours(17), end: this.week.addDays(1).addHours(18), tags: { category: "delivery" } },
{ id: "opening", resource: "C", text: "Team stand-up", start: this.week.addDays(2).addHours(8), end: this.week.addDays(2).addHours(9), tags: { category: "planning" } },
{ id: "weekend", resource: "A", text: "Weekend inspection", start: this.week.addDays(5).addHours(9), end: this.week.addDays(5).addHours(11), tags: { category: "support" } },
{ id: "standup-mon", resource: "B", text: "Team stand-up", start: this.week.addHours(9), end: this.week.addHours(10), tags: { category: "planning" } },
{ id: "interviews", resource: "C", text: "Interviews", start: this.week.addHours(11), end: this.week.addHours(13), tags: { category: "research" } },
{ id: "client-call", resource: "B", text: "Client call", start: this.week.addHours(14), end: this.week.addHours(15), tags: { category: "delivery" } },
{ id: "planning", resource: "C", text: "Sprint planning", start: this.week.addDays(1).addHours(14), end: this.week.addDays(1).addHours(16), tags: { category: "planning" } },
{ id: "design", resource: "A", text: "Design review", start: this.week.addDays(2).addHours(13), end: this.week.addDays(2).addHours(15), tags: { category: "design" } },
{ id: "demo", resource: "B", text: "Product demo", start: this.week.addDays(3).addHours(10), end: this.week.addDays(3).addHours(12), tags: { category: "delivery" } },
{ id: "retro", resource: "C", text: "Retrospective", start: this.week.addDays(4).addHours(15), end: this.week.addDays(4).addHours(17), tags: { category: "research" } },
{ id: "kickoff", resource: "D", text: "Project kick-off", start: this.week.addHours(9), end: this.week.addHours(11), tags: { category: "planning" } },
{ id: "candidate", resource: "D", text: "Candidate interviews", start: this.week.addHours(13), end: this.week.addHours(15), tags: { category: "research" } },
{ id: "product", resource: "D", text: "Product review", start: this.week.addDays(1).addHours(10), end: this.week.addDays(1).addHours(12), tags: { category: "design" } },
{ id: "critique", resource: "D", text: "Design critique", start: this.week.addDays(1).addHours(16), end: this.week.addDays(1).addHours(18), tags: { category: "design" } },
{ id: "birch-standup", resource: "E", text: "Daily stand-up", start: this.week.addHours(8), end: this.week.addHours(9), tags: { category: "planning" } },
{ id: "roadmap", resource: "E", text: "Roadmap planning", start: this.week.addHours(11), end: this.week.addHours(13), tags: { category: "planning" } },
{ id: "customer", resource: "E", text: "Customer call", start: this.week.addHours(15), end: this.week.addHours(16), tags: { category: "delivery" } },
{ id: "birch-interview", resource: "E", text: "Research interviews", start: this.week.addDays(1).addHours(9), end: this.week.addDays(1).addHours(11), tags: { category: "research" } },
{ id: "all-hands", resource: "F", text: "All-hands", start: this.week.addHours(10), end: this.week.addHours(12), tags: { category: "planning" } },
{ id: "training", resource: "F", text: "Team training", start: this.week.addHours(14), end: this.week.addHours(17), tags: { category: "design" } },
{ id: "leadership", resource: "F", text: "Leadership sync", start: this.week.addDays(1).addHours(8), end: this.week.addDays(1).addHours(9), tags: { category: "delivery" } },
{ id: "elm-review", resource: "F", text: "Quarterly review", start: this.week.addDays(1).addHours(13), end: this.week.addDays(1).addHours(15), tags: { category: "delivery" } },
{ id: "strategy", resource: "D", text: "Strategy workshop", start: this.week.addDays(2).addHours(9), end: this.week.addDays(2).addHours(12), tags: { category: "planning" } },
{ id: "partner", resource: "E", text: "Partner meeting", start: this.week.addDays(3).addHours(14), end: this.week.addDays(3).addHours(16), tags: { category: "delivery" } },
{ id: "showcase", resource: "F", text: "Friday showcase", start: this.week.addDays(4).addHours(11), end: this.week.addDays(4).addHours(13), tags: { category: "design" } }
];
},
changed(action, event) {
const start = event.start().toString("ddd M/d H:mm");
const end = event.end().toString("ddd M/d H:mm");
this.elements.status.textContent = `${action}: ${event.text()} · ${start} – ${end}`;
this.showPeriod();
},
showPeriod() {
this.elements.period.textContent = this.week.toString("MMM d") + " – " + this.week.addDays(6).toString("MMM d, yyyy");
},
navigate(weeks) {
this.eventsByPeriod.set(this.week.toString(), dp.events.list);
this.week = this.week.addDays(weeks * 7);
const events = this.eventsByPeriod.get(this.week.toString()) || this.events();
dp.clearSelection();
dp.update({startDate: this.week, events});
dp.scrollToY(0);
this.showPeriod();
}
};
const dp = new DayPilot.Calendar("dp", {
startDate: app.week,
days: 7,
viewType: "Resources",
scale: "Hour",
heightSpec: "Fixed",
height: 520,
cellHeight: 32,
hourWidth: 100,
businessBeginsHour: 8,
businessEndsHour: 18,
businessWeekends: true,
useEventBoxes: "Always",
eventBorderRadius: 5,
columnMarginLeft: 2,
columnMarginRight: 4,
eventMoveSkipNonBusiness: true,
eventMovingStartEndEnabled: true,
eventResizingStartEndEnabled: true,
eventMovingStartEndFormat: "ddd H:mm",
eventResizingStartEndFormat: "ddd H:mm",
columns: [
{ id: "A", name: "Cedar · 6 seats" }, { id: "B", name: "Maple · 8 seats" },
{ id: "C", name: "Willow · 4 seats" }, { id: "D", name: "Oak · 10 seats" },
{ id: "E", name: "Birch · 6 seats" }, { id: "F", name: "Elm · 12 seats" }
],
events: app.events(),
...app.filter(),
timeHeaders: [{ groupBy: "Day", format: "ddd M/d", width: 80 }, { groupBy: "Hour", format: "H:mm", width: 60 }],
onBeforeEventRender: args => app.renderEvent(args),
onBeforeEventExport: args => app.exportEvent(args),
onTimeRangeSelected: async (args) => {
const modal = await DayPilot.Modal.prompt("New booking:", "Booking");
dp.clearSelection();
if (modal.canceled || !modal.result) {
return;
}
const event = new DayPilot.Event({
id: DayPilot.guid(),
resource: args.resource,
text: modal.result,
start: args.start,
end: args.end,
tags: { category: "planning" }
});
dp.events.add(event);
app.changed("Created", event);
},
onEventMoved: args => app.changed("Moved", args.e),
onEventResized: args => app.changed("Resized", args.e)
});
dp.init();
app.init();