Skip to content

lightweight, client-only calendar web application. All data persists in the URL hash for instant sharing, No backend required. Optional AES-GCM password protection keeps shared links locked without a server

JavaScript

fix: update 24h module

Supun Lakmalbb55d2db5701
diff --git a/index.html b/index.html
index e691990..173909a 100644
--- a/index.html
+++ b/index.html
@@ -198,6 +198,10 @@
<i class="fa-solid fa-bell" aria-hidden="true"></i>
<span class="menu-item-label" data-i18n="settings.notificationsOff">Notifications: Off</span>
</button>
+ <button id="timeformat-toggle" type="button" class="menu-text-item">
+ <i class="fa-solid fa-clock" aria-hidden="true"></i>
+ <span class="menu-item-label" data-i18n="settings.timeFormat12h">Time: 12h</span>
+ </button>
<button id="focus-btn" type="button" class="menu-text-item">
<i class="fa-solid fa-bolt" aria-hidden="true"></i>
<span class="menu-item-label" data-i18n="btn.focus">Focus</span>
@@ -1007,6 +1011,9 @@
<button id="mobile-notify-toggle" type="button" class="mobile-drawer-item" aria-label="Toggle notifications">
<i class="fa-solid fa-bell"></i> <span data-i18n="settings.notificationsOff">Notifications: Off</span>
</button>
+ <button id="mobile-timeformat-toggle" type="button" class="mobile-drawer-item" aria-label="Toggle time format">
+ <i class="fa-solid fa-clock"></i> <span data-i18n="settings.timeFormat12h">Time: 12h</span>
+ </button>
<button id="mobile-readonly-btn" type="button" class="mobile-drawer-item" aria-pressed="false">
<i class="fa-solid fa-pen"></i> <span data-i18n="btn.readOnly">Read only</span>
</button>
diff --git a/locales/en.js b/locales/en.js
index 73e92f4..91d6ccd 100644
--- a/locales/en.js
+++ b/locales/en.js
@@ -171,6 +171,8 @@ export default {
"settings.themeDark": "Theme: Dark",
"settings.notificationsOff": "Notifications: Off",
"settings.notificationsOn": "Notifications: On",
+ "settings.timeFormat12h": "Time: 12h",
+ "settings.timeFormat24h": "Time: 24h",
"settings.notificationsBlocked": "Notifications: Blocked",
"settings.notificationsUnsupported": "Notifications unsupported",
"settings.language": "Language",
diff --git a/modules/agendaRender.js b/modules/agendaRender.js
index 18b1415..3c8ca81 100644
--- a/modules/agendaRender.js
+++ b/modules/agendaRender.js
@@ -30,8 +30,10 @@ function formatAgendaHeader(date) {
});
}
+let _is24h = false;
+
function formatAgendaTime(date) {
- return date.toLocaleTimeString(getCurrentLocale(), { hour: "2-digit", minute: "2-digit" });
+ return date.toLocaleTimeString(getCurrentLocale(), { hour: "2-digit", minute: "2-digit", hour12: !_is24h });
}
function decorateOccurrences(occurrences, colors) {
@@ -86,7 +88,8 @@ function buildAgendaData({ events, colors, rangeMonths, occurrences }) {
return { occurrences: decoratedOccurrences, occurrencesByDay, range: { start: rangeStart, end: rangeEnd } };
}
-export function renderAgendaView({ events, colors, container, rangeMonths = 6, onEventClick, occurrences } = {}) {
+export function renderAgendaView({ events, colors, container, rangeMonths = 6, is24h = false, onEventClick, occurrences } = {}) {
+ _is24h = is24h;
const data = buildAgendaData({ events, colors, rangeMonths, occurrences });
if (!container) return data;
diff --git a/modules/cacheElements.js b/modules/cacheElements.js
index 18f400f..ce12bda 100644
--- a/modules/cacheElements.js
+++ b/modules/cacheElements.js
@@ -18,6 +18,7 @@ export function cacheElements(ui) {
ui.weekstartToggle = document.getElementById("weekstart-toggle");
ui.themeToggle = document.getElementById("theme-toggle");
ui.notifyToggle = document.getElementById("notify-toggle");
+ ui.timeformatToggle = document.getElementById("timeformat-toggle");
ui.langBtn = document.getElementById("language-btn");
ui.langList = document.getElementById("language-list");
ui.currentLang = document.getElementById("current-lang");
@@ -162,6 +163,7 @@ export function cacheElements(ui) {
ui.mobileWeekstartToggle = document.getElementById("mobile-weekstart-toggle");
ui.mobileThemeToggle = document.getElementById("mobile-theme-toggle");
ui.mobileNotifyToggle = document.getElementById("mobile-notify-toggle");
+ ui.mobileTimeformatToggle = document.getElementById("mobile-timeformat-toggle");
ui.mobileReadOnlyBtn = document.getElementById("mobile-readonly-btn");
ui.mobileLangBtn = document.getElementById("mobile-language-btn");
ui.mobileLangList = document.getElementById("mobile-language-list");
diff --git a/modules/calendarRender.js b/modules/calendarRender.js
index 4558325..0ce29b8 100644
--- a/modules/calendarRender.js
+++ b/modules/calendarRender.js
@@ -143,13 +143,16 @@ export function renderCalendar({
});
}
-function formatHourLabel(hour) {
+function formatHourLabel(hour, is24h = false) {
+ if (is24h) {
+ return `${String(hour).padStart(2, "0")}:00`;
+ }
const suffix = hour < 12 ? "AM" : "PM";
const display = hour % 12 || 12;
return `${display} ${suffix}`;
}
-export function renderTimeGrid({ container, dates, occurrences, onSelectDay, onEventClick }) {
+export function renderTimeGrid({ container, dates, occurrences, onSelectDay, onEventClick, is24h = false }) {
const dayKeys = dates.map((date) => formatDateKey(date));
const buckets = new Map();
dayKeys.forEach((key) => {
@@ -222,7 +225,7 @@ export function renderTimeGrid({ container, dates, occurrences, onSelectDay, onE
for (let hour = 0; hour < 24; hour += 1) {
const label = document.createElement("div");
label.className = "time-label";
- label.textContent = formatHourLabel(hour);
+ label.textContent = formatHourLabel(hour, is24h);
// Each hour spans 60 minutes
const startRow = 2 + hour * 60;
label.style.gridRow = `${startRow} / span 60`;
diff --git a/modules/countdownManager.js b/modules/countdownManager.js
index 1acc2d2..8fef1a9 100644
--- a/modules/countdownManager.js
+++ b/modules/countdownManager.js
@@ -8,6 +8,7 @@ let countdownInterval = null;
let latestEvents = [];
let cachedElements = null;
let lastEventKey = null;
+let _is24h = false;
function addDays(date, days) {
const next = new Date(date.getTime());
@@ -20,7 +21,7 @@ function isSameDay(a, b) {
}
function formatTime(date) {
- return date.toLocaleTimeString(getCurrentLocale(), { hour: "2-digit", minute: "2-digit" });
+ return date.toLocaleTimeString(getCurrentLocale(), { hour: "2-digit", minute: "2-digit", hour12: !_is24h });
}
function formatDateLabel(date) {
@@ -145,7 +146,8 @@ function updateWidgetUI(event) {
countdownInterval = window.setInterval(tick, 1000);
}
-export function initCountdownWidget(events = []) {
+export function initCountdownWidget(events = [], { is24h = false } = {}) {
+ _is24h = is24h;
latestEvents = Array.isArray(events) ? events : [];
const nextEvent = findNextEvent(latestEvents);
const eventKey = nextEvent ? `${nextEvent.start}|${nextEvent.title}|${nextEvent.isAllDay ? 1 : 0}` : "none";
diff --git a/modules/focusMode.js b/modules/focusMode.js
index 69c3753..731aa5b 100644
--- a/modules/focusMode.js
+++ b/modules/focusMode.js
@@ -25,8 +25,10 @@ function isSameDay(a, b) {
);
}
+let _is24h = false;
+
function formatTime(date) {
- return date.toLocaleTimeString(getCurrentLocale(), { hour: "2-digit", minute: "2-digit" });
+ return date.toLocaleTimeString(getCurrentLocale(), { hour: "2-digit", minute: "2-digit", hour12: !_is24h });
}
function formatDateLabel(date) {
@@ -131,6 +133,8 @@ export class FocusMode {
tick() {
if (!this.container) return;
+ const state = typeof this.getState === "function" ? this.getState() : null;
+ _is24h = !!(state && state.mp && state.mp.f24);
const now = new Date();
if (this.clockEl) this.clockEl.textContent = formatTime(now);
diff --git a/modules/responsiveFeatures.js b/modules/responsiveFeatures.js
index a4b03fe..a09cbec 100644
--- a/modules/responsiveFeatures.js
+++ b/modules/responsiveFeatures.js
@@ -57,6 +57,7 @@ export function createResponsiveFeaturesController({ ui, cssClasses, t } = {}) {
openTzModal,
setView,
handleWeekStartToggle,
+ handleTimeFormatToggle,
handleThemeToggle,
handleNotificationToggle,
handleReadOnlyToggle,
@@ -154,6 +155,9 @@ export function createResponsiveFeaturesController({ ui, cssClasses, t } = {}) {
if (ui.mobileWeekstartToggle && typeof handleWeekStartToggle === "function") {
ui.mobileWeekstartToggle.addEventListener("click", handleWeekStartToggle);
}
+ if (ui.mobileTimeformatToggle && typeof handleTimeFormatToggle === "function") {
+ ui.mobileTimeformatToggle.addEventListener("click", handleTimeFormatToggle);
+ }
if (ui.mobileThemeToggle && typeof handleThemeToggle === "function") {
ui.mobileThemeToggle.addEventListener("click", handleThemeToggle);
}
diff --git a/modules/timelineRender.js b/modules/timelineRender.js
index ad6d8d4..6b1c105 100644
--- a/modules/timelineRender.js
+++ b/modules/timelineRender.js
@@ -47,12 +47,12 @@ function formatHourMarker(hour) {
return `${String(hour).padStart(2, "0")}:00`;
}
-function formatEventTimeRange(event, locale, allDayLabel) {
+function formatEventTimeRange(event, locale, allDayLabel, is24h = false) {
if (event.isAllDay) return allDayLabel;
const startDate = new Date(event.start);
const endDate = new Date(event.end);
const sameDay = formatDateKey(startDate) === formatDateKey(endDate);
- const timeOptions = { hour: "2-digit", minute: "2-digit" };
+ const timeOptions = { hour: "2-digit", minute: "2-digit", hour12: !is24h };
const startTime = startDate.toLocaleTimeString(locale, timeOptions);
const endTime = endDate.toLocaleTimeString(locale, timeOptions);
@@ -126,6 +126,7 @@ export function renderTimelineView({
selectedDate,
dayWidth = 64,
locale = "en-US",
+ is24h = false,
allDayLabel = "All day",
emptyLabel = "No upcoming events",
onSelectDay,
@@ -304,7 +305,7 @@ export function renderTimelineView({
const timeLabel = document.createElement("span");
timeLabel.className = "timeline-event-time";
- timeLabel.textContent = formatEventTimeRange(event, locale, allDayLabel);
+ timeLabel.textContent = formatEventTimeRange(event, locale, allDayLabel, is24h);
const title = document.createElement("span");
title.className = "timeline-event-title";
diff --git a/modules/timezoneManager.js b/modules/timezoneManager.js
index 6e1e421..7b48468 100644
--- a/modules/timezoneManager.js
+++ b/modules/timezoneManager.js
@@ -61,13 +61,14 @@ function formatUtcOffset(offsetMinutes) {
return `${sign}${hours}:${String(minutes).padStart(2, "0")}`;
}
-export function getZoneInfo(zoneName) {
+export function getZoneInfo(zoneName, { is24h = false } = {}) {
const now = new Date();
const timeFormatter = new Intl.DateTimeFormat(undefined, {
timeZone: zoneName,
hour: "numeric",
minute: "2-digit",
+ hour12: !is24h,
});
const localDateKey = formatDateKey(now);
diff --git a/modules/worldPlannerModule.js b/modules/worldPlannerModule.js
index acfd22e..d86ebdf 100644
--- a/modules/worldPlannerModule.js
+++ b/modules/worldPlannerModule.js
@@ -333,7 +333,9 @@ export class WorldPlanner {
const div = document.createElement("div");
div.className = `wp-row-header${isHome ? " home-row" : ""}`;
- const info = getZoneInfo(zone);
+ const state = this.getState();
+ const is24h = !!(state && state.mp && state.mp.f24);
+ const info = getZoneInfo(zone, { is24h });
// Controls
if (!isHome) {
diff --git a/script.js b/script.js
index a8b49e6..7f888e2 100644
--- a/script.js
+++ b/script.js
@@ -215,9 +215,8 @@ function setCalendarHeaderLabel(text) {
}
function formatTime(date) {
- // Use Intl for time as it is usually reliable (numbers/colons) or we can just stick to it.
- // Actually, let's stick to locale sensitive time, it usually works fine (HH:mm).
- return date.toLocaleTimeString(getCurrentLocale(), { hour: "2-digit", minute: "2-digit" });
+ const is24h = !!(state.mp && state.mp.f24);
+ return date.toLocaleTimeString(getCurrentLocale(), { hour: "2-digit", minute: "2-digit", hour12: !is24h });
}
function filterOccurrences(occurrences) {
@@ -357,7 +356,8 @@ function hasStoredData() {
}
function createTzCard(zoneId, isLocal, isUTC = false) {
- const data = getZoneInfo(zoneId);
+ const is24h = !!(state.mp && state.mp.f24);
+ const data = getZoneInfo(zoneId, { is24h });
const div = document.createElement("div");
div.className = `tz-card${isLocal ? " is-local" : ""}${isUTC ? " is-utc" : ""}`;
@@ -1537,6 +1537,7 @@ function updateLanguageUI() {
// Re-translate components
updateTheme();
updateWeekStartLabel();
+ updateTimeFormatLabel();
updateLockUI();
updateFocusButton();
updateViewButtons();
@@ -1582,6 +1583,7 @@ function decorateOccurrences(occurrences) {
function render() {
updateTheme();
updateWeekStartLabel();
+ updateTimeFormatLabel();
updateNotificationToggleLabel();
// Dropdown UI is updated via updateLanguageUI which is called separately
if (ui.titleInput && document.activeElement !== ui.titleInput) {
@@ -1644,6 +1646,7 @@ function render() {
container: ui.calendarGrid,
dates: range.dates,
occurrences: decorated,
+ is24h: !!(state.mp && state.mp.f24),
onSelectDay: handleSelectDay,
onEventClick: (event) => {
if (eventCrudController) {
@@ -1666,6 +1669,7 @@ function render() {
container: ui.calendarGrid,
dates: [start],
occurrences: decorated,
+ is24h: !!(state.mp && state.mp.f24),
onSelectDay: handleSelectDay,
onEventClick: (event) => {
if (eventCrudController) {
@@ -1683,6 +1687,7 @@ function render() {
colors: state.c,
container: ui.calendarGrid,
rangeMonths: 6,
+ is24h: !!(state.mp && state.mp.f24),
occurrences: filteredAgendaOccurrences,
onEventClick: (event) => {
if (eventCrudController) {
@@ -1710,6 +1715,7 @@ function render() {
selectedDate,
dayWidth: getTimelineDayWidth(),
locale: getCurrentLocale(),
+ is24h: !!(state.mp && state.mp.f24),
allDayLabel: t("calendar.allDay"),
emptyLabel: t("calendar.noUpcoming"),
onSelectDay: handleSelectDay,
@@ -1777,7 +1783,7 @@ function render() {
updateJsonModal();
}
updateLockUI();
- initCountdownWidget(state.e);
+ initCountdownWidget(state.e, { is24h: !!(state.mp && state.mp.f24) });
if (eventSearchController && eventSearchController.isOpen()) {
renderEventSearchResults();
}
@@ -2021,6 +2027,30 @@ function handleWeekStartToggle() {
scheduleSave();
}
+function handleTimeFormatToggle() {
+ if (!state.mp) state.mp = { h: null, z: ["UTC"], s: null, d: null, f24: false };
+ state.mp.f24 = !state.mp.f24;
+ render();
+ scheduleSave();
+}
+
+function updateTimeFormatLabel() {
+ const is24h = !!(state.mp && state.mp.f24);
+ const label = t(is24h ? "settings.timeFormat24h" : "settings.timeFormat12h");
+ if (ui.timeformatToggle) {
+ const span = ui.timeformatToggle.querySelector(".menu-item-label");
+ if (span) {
+ span.textContent = label;
+ } else {
+ ui.timeformatToggle.textContent = label;
+ }
+ }
+ if (ui.mobileTimeformatToggle) {
+ const span = ui.mobileTimeformatToggle.querySelector("span");
+ if (span) span.textContent = label;
+ }
+}
+
function shiftView(direction) {
if (currentView === "month") {
viewDate = new Date(viewDate.getFullYear(), viewDate.getMonth() + direction, 1);
@@ -2467,6 +2497,8 @@ function bindEvents() {
if (ui.weekstartToggle) ui.weekstartToggle.addEventListener("click", handleWeekStartToggle);
if (ui.themeToggle) ui.themeToggle.addEventListener("click", handleThemeToggle);
if (ui.notifyToggle) ui.notifyToggle.addEventListener("click", handleNotificationToggle);
+ if (ui.timeformatToggle) ui.timeformatToggle.addEventListener("click", handleTimeFormatToggle);
+ if (ui.mobileTimeformatToggle) ui.mobileTimeformatToggle.addEventListener("click", handleTimeFormatToggle);
if (ui.timelineZoomOut) ui.timelineZoomOut.addEventListener("click", () => handleTimelineZoomStep(-1));
if (ui.timelineZoomIn) ui.timelineZoomIn.addEventListener("click", () => handleTimelineZoomStep(1));
if (ui.timelineZoomRange) ui.timelineZoomRange.addEventListener("input", handleTimelineZoomInput);
@@ -2556,6 +2588,7 @@ function initResponsiveFeatures() {
openTzModal,
setView,
handleWeekStartToggle,
+ handleTimeFormatToggle,
handleThemeToggle,
handleNotificationToggle,
handleReadOnlyToggle,