Admin: dates always read day/month/year

Native date fields follow the browser's language, so in a US-English
browser they showed mm/dd/yyyy and a page can't change that. DateField
shows dd/mm/yyyy (typed, or picked from the native calendar behind a
button) and posts YYYY-MM-DD as before; DateTimeField pairs it with a
time field in place of datetime-local. Used in Slots (window, session,
generate, copy, clear), Events and the code expiry.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Hy2vMnHJYP7QGsiH21zHxW
This commit is contained in:
agent 2026-10-26 20:22:43 +00:00
parent 2c0f54b1f7
commit f8b4c19586
9 changed files with 210 additions and 13 deletions

View file

@ -0,0 +1,125 @@
<script lang="ts">
import { untrack } from 'svelte';
/**
* A date field that always reads day/month/year (31/12/2026). A native
* <input type="date"> shows the browser's own format (month/day/year in US
* English) and a page can't change that, so the date is typed here and a
* hidden native one only supplies the calendar behind the button.
* `value` is YYYY-MM-DD, as a native date field's, and is what the form gets as `name`.
*/
let {
value = $bindable(''),
name,
required = false,
min = '',
disabled = false,
class: className = 'w-full',
label,
onChange
} = $props<{
value?: string;
/** Leave it out where a parent posts the value (DateTimeField). */
name?: string;
required?: boolean;
/** YYYY-MM-DD; earlier dates can't be picked or typed. */
min?: string;
disabled?: boolean;
class?: string;
/** For screen readers, where the field has no visible label of its own. */
label?: string;
onChange?: (value: string) => void;
}>();
let text = $state(toText(value));
let textInput = $state<HTMLInputElement>();
let picker = $state<HTMLInputElement>();
/** YYYY-MM-DD → 31/12/2026 ('' if it isn't a date). */
function toText(iso: string) {
const match = iso?.match(/^(\d{4})-(\d{2})-(\d{2})/);
return match ? `${match[3]}/${match[2]}/${match[1]}` : '';
}
/** 31/12/2026, 31.12.2026, 1-2-2026 … → YYYY-MM-DD ('' if it isn't a real date). */
function fromText(typed: string) {
const match = typed.trim().match(/^(\d{1,2})\D(\d{1,2})\D(\d{4})$/);
if (!match) return '';
const [day, month, year] = [Number(match[1]), Number(match[2]), Number(match[3])];
const date = new Date(Date.UTC(year, month - 1, day));
if (date.getUTCMonth() !== month - 1 || date.getUTCDate() !== day) return '';
return `${year}-${String(month).padStart(2, '0')}-${String(day).padStart(2, '0')}`;
}
function set(next: string) {
if (next === value) return;
value = next;
onChange?.(next);
}
// A new value from outside (a reset, another field) replaces what's typed,
// but half-typed text that doesn't make a date yet is left alone.
$effect(() => {
const current = value;
untrack(() => {
if (fromText(text) !== current) text = toText(current);
});
});
// Typed text that isn't a date (or is before `min`) stops the form, with a reason.
$effect(() => {
const parsed = fromText(text);
let message = '';
if (text.trim() && !parsed) message = 'Type the date as day/month/year, for example 31/12/2026.';
else if (parsed && min && parsed < min) message = `Choose ${toText(min)} or later.`;
textInput?.setCustomValidity(message);
});
function openCalendar() {
if (!picker) return;
try {
picker.showPicker();
} catch {
// Older browsers: the field takes focus and opens its own calendar.
picker.focus();
picker.click();
}
}
</script>
<div class="input relative {className}">
<input
class="min-w-0 grow"
type="text"
inputmode="numeric"
autocomplete="off"
placeholder="dd/mm/yyyy"
aria-label={label}
{required}
{disabled}
bind:this={textInput}
bind:value={text}
oninput={() => set(fromText(text))}
onblur={() => {
// Tidy 1/2/2026 into 01/02/2026 once it's a date.
if (value && fromText(text) === value) text = toText(value);
}}
/>
<button class="btn btn-ghost btn-xs btn-square -mr-1 shrink-0" type="button" aria-label="Choose from a calendar" title="Choose from a calendar" {disabled} onclick={openCalendar}>
<svg class="size-4 opacity-70" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="1.5" aria-hidden="true">
<path stroke-linecap="round" stroke-linejoin="round" d="M6.75 3v2.25M17.25 3v2.25M3 18.75V7.5a2.25 2.25 0 0 1 2.25-2.25h13.5A2.25 2.25 0 0 1 21 7.5v11.25m-18 0A2.25 2.25 0 0 0 5.25 21h13.5A2.25 2.25 0 0 0 21 18.75m-18 0v-7.5A2.25 2.25 0 0 1 5.25 9h13.5A2.25 2.25 0 0 1 21 11.25v7.5" />
</svg>
</button>
<!-- Only for its calendar: out of sight under the button, so the calendar opens there. -->
<input
class="pointer-events-none absolute right-0 bottom-0 h-full w-10 opacity-0"
type="date"
tabindex="-1"
aria-hidden="true"
min={min || undefined}
{value}
bind:this={picker}
onchange={(e) => set(e.currentTarget.value)}
/>
{#if name && !disabled}<input type="hidden" {name} {value} />{/if}
</div>

View file

@ -0,0 +1,57 @@
<script lang="ts">
import { untrack } from 'svelte';
import DateField from './DateField.svelte';
/**
* A date (day/month/year, see DateField) and a time, in place of a native
* <input type="datetime-local">, whose date follows the browser's format.
* `value` is YYYY-MM-DDTHH:mm, as a native one's, and is what the form gets as `name`.
*/
let {
value = $bindable(''),
name,
required = false,
min = '',
disabled = false,
onChange
} = $props<{
value?: string;
name: string;
required?: boolean;
/** Earliest date (a date or date-time; only its date is used). */
min?: string;
disabled?: boolean;
onChange?: (value: string) => void;
}>();
let date = $state(value.slice(0, 10));
let time = $state(value.slice(11, 16));
function joined() {
return date && time ? `${date}T${time}` : '';
}
// A new value from outside replaces both parts; a half-filled pair (value '') is left alone.
$effect(() => {
const current = value;
untrack(() => {
if (joined() !== current) {
date = current.slice(0, 10);
time = current.slice(11, 16);
}
});
});
function update() {
const next = joined();
if (next === value) return;
value = next;
onChange?.(next);
}
</script>
<div class="flex gap-2">
<DateField class="min-w-0 flex-1" label="Date" value={date} min={min.slice(0, 10)} {required} {disabled} onChange={(d) => { date = d; update(); }} />
<input class="input w-28 shrink-0" type="time" aria-label="Time" {required} {disabled} bind:value={time} oninput={update} />
{#if !disabled}<input type="hidden" {name} {value} />{/if}
</div>

View file

@ -4,6 +4,7 @@
import Modal from '$lib/admin/Modal.svelte';
import BulkActions from './BulkActions.svelte';
import { bulkForm } from './bulkForm.svelte';
import DateField from '$lib/admin/DateField.svelte';
let {
open = $bindable(false),
@ -38,11 +39,11 @@
<div class="grid gap-x-4 md:grid-cols-2">
<label class="fieldset">
<span class="fieldset-legend">From</span>
<input class="input w-full" type="date" name="fromDate" required bind:value={draft.fromDate} />
<DateField name="fromDate" required bind:value={draft.fromDate} />
</label>
<label class="fieldset">
<span class="fieldset-legend">To</span>
<input class="input w-full" type="date" name="toDate" required min={draft.fromDate} bind:value={draft.toDate} />
<DateField name="toDate" required min={draft.fromDate} bind:value={draft.toDate} />
</label>
</div>

View file

@ -5,6 +5,7 @@
import { addDaysToKey, formatDateKey, mondayOfKey } from '$lib/slots';
import BulkActions from './BulkActions.svelte';
import { bulkForm } from './bulkForm.svelte';
import DateField from '$lib/admin/DateField.svelte';
let {
open = $bindable(false),
@ -41,7 +42,7 @@
<div class="grid gap-x-4 md:grid-cols-2">
<label class="fieldset">
<span class="fieldset-legend">Copy the week of</span>
<input class="input w-full" type="date" name="sourceDate" required bind:value={draft.sourceDate} />
<DateField name="sourceDate" required bind:value={draft.sourceDate} />
{#if monday}<span class="label">{shortDate(monday)} – {shortDate(addDaysToKey(monday, 6))}</span>{/if}
</label>
<label class="fieldset">

View file

@ -8,6 +8,7 @@
import { formatDuration } from '$shared/items';
import BulkActions from './BulkActions.svelte';
import { bulkForm } from './bulkForm.svelte';
import DateField from '$lib/admin/DateField.svelte';
type Service = { id: string; name: string; durations: ItemDuration[]; bufferMinutes: number; capacity: number };
@ -95,11 +96,11 @@
<div class="grid gap-x-4 md:grid-cols-2">
<label class="fieldset">
<span class="fieldset-legend">From</span>
<input class="input w-full" type="date" name="fromDate" required bind:value={draft.fromDate} />
<DateField name="fromDate" required bind:value={draft.fromDate} />
</label>
<label class="fieldset">
<span class="fieldset-legend">To</span>
<input class="input w-full" type="date" name="toDate" required min={draft.fromDate} bind:value={draft.toDate} />
<DateField name="toDate" required min={draft.fromDate} bind:value={draft.toDate} />
</label>
</div>

View file

@ -1,6 +1,8 @@
<script lang="ts">
import { addMonths, createCalendarMonth, eventOverlapsDay, formatEventDateRange, sortEvents, toDateInput, toZonedDatetimeInput } from '$lib/calendar';
import { confirmSubmit } from '$lib/admin/confirm';
import DateField from '$lib/admin/DateField.svelte';
import DateTimeField from '$lib/admin/DateTimeField.svelte';
import Modal from '$lib/admin/Modal.svelte';
import type { CalendarEventRecord } from '$lib/types';
@ -25,7 +27,6 @@
const weekdays = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'];
let month = $derived(createCalendarMonth(viewDate, new Date(), 1));
let action = $derived(draft.id ? '?/update' : '?/create');
let dateInputType = $derived(draft.allDay ? 'date' : 'datetime-local');
let startValue = $derived(draft.allDay ? toDateInput(draft.start) : toZonedDatetimeInput(draft.start, data.timezone));
let endValue = $derived(draft.allDay ? toDateInput(draft.end) : toZonedDatetimeInput(draft.end, data.timezone));
let allCategories = $derived([...new Set(events.flatMap((event) => event.categories || []))].sort((a, b) => a.localeCompare(b)));
@ -168,11 +169,19 @@
<div class="grid gap-x-4 md:grid-cols-2">
<label class="fieldset">
<span class="fieldset-legend">Start{draft.allDay ? '' : ` (${data.timezone})`}</span>
<input class="input w-full" type={dateInputType} name="start" required value={startValue} oninput={(e) => (draft = { ...draft, start: e.currentTarget.value })} />
{#if draft.allDay}
<DateField name="start" required value={startValue} onChange={(v) => (draft = { ...draft, start: v })} />
{:else}
<DateTimeField name="start" required value={startValue} onChange={(v) => (draft = { ...draft, start: v })} />
{/if}
</label>
<label class="fieldset">
<span class="fieldset-legend">End <span class="font-normal text-base-content/50">(optional)</span></span>
<input class="input w-full" type={dateInputType} name="end" min={startValue} value={endValue} oninput={(e) => (draft = { ...draft, end: e.currentTarget.value })} />
{#if draft.allDay}
<DateField name="end" min={startValue} value={endValue} onChange={(v) => (draft = { ...draft, end: v })} />
{:else}
<DateTimeField name="end" min={startValue} value={endValue} onChange={(v) => (draft = { ...draft, end: v })} />
{/if}
</label>
</div>

View file

@ -4,6 +4,7 @@
import type { PassPart } from '$lib/types';
import { formatDateTime, toZonedDatetimeInput } from '$lib/calendar';
import { confirmSubmit } from '$lib/admin/confirm';
import DateField from '$lib/admin/DateField.svelte';
import Modal from '$lib/admin/Modal.svelte';
import { formatDuration } from '$shared/items';
@ -212,7 +213,7 @@
<input type="hidden" name="id" value={managing.id} />
<label class="fieldset py-0">
<span class="fieldset-legend">Expires ({data.timezone})</span>
<input class="input w-48" type="date" name="expiresAt" bind:value={expiryDraft} />
<DateField class="w-48" name="expiresAt" bind:value={expiryDraft} />
</label>
<button class="btn" type="submit">Save expiry</button>
<button class="btn btn-ghost" type="submit" name="never" value="1">Never expires</button>

View file

@ -12,6 +12,8 @@
import type { BulkResult, SlotState } from '$lib/slots';
import type { SlotKind, StartTimesMode } from '$lib/types';
import { formatDuration } from '$shared/items';
import DateField from '$lib/admin/DateField.svelte';
import DateTimeField from '$lib/admin/DateTimeField.svelte';
let { data, form } = $props();
let items = $derived(data.items || []);
@ -428,7 +430,7 @@
<div class="grid gap-x-4 md:grid-cols-3">
<label class="fieldset">
<span class="fieldset-legend">Date</span>
<input class="input w-full" type="date" name="date" required bind:value={draft.date} />
<DateField name="date" required bind:value={draft.date} />
</label>
<label class="fieldset">
<span class="fieldset-legend">From ({data.timezone})</span>
@ -502,7 +504,7 @@
</label>
<label class="fieldset">
<span class="fieldset-legend">Start ({data.timezone})</span>
<input class="input w-full" type="datetime-local" name="start" required disabled={sessionLocked} bind:value={draft.start} />
<DateTimeField name="start" required disabled={sessionLocked} bind:value={draft.start} />
{#if endLabel}<span class="label">{endLabel}</span>{/if}
</label>
<label class="fieldset">