Files
nat-as-server/website/js/datetime.js
tom.hempel f04388e0ec
Some checks failed
PHPUnit / test (push) Has been cancelled
changes to translation system
2026-07-09 14:59:56 +02:00

32 lines
1.2 KiB
JavaScript

/** App timezone for display (Germany: CET/CEST). */
export const APP_TIMEZONE = 'Europe/Berlin';
/** YYYY-MM-DD in app timezone (date inputs, log filters, filename stamps). */
export function berlinDateInputValue(date = new Date()) {
return new Intl.DateTimeFormat('en-CA', { timeZone: APP_TIMEZONE }).format(date);
}
/** YYYY-MM-DD HH:mm:ss in app timezone. */
export function formatBerlinDateTime(value) {
const d = value instanceof Date ? value : new Date(value);
if (Number.isNaN(d.getTime())) return '';
const parts = new Intl.DateTimeFormat('en-GB', {
timeZone: APP_TIMEZONE,
year: 'numeric',
month: '2-digit',
day: '2-digit',
hour: '2-digit',
minute: '2-digit',
second: '2-digit',
hour12: false,
}).formatToParts(d);
const get = (type) => parts.find((p) => p.type === type)?.value ?? '';
return `${get('year')}-${get('month')}-${get('day')} ${get('hour')}:${get('minute')}:${get('second')}`;
}
/** YYYY-MM-DD HH:mm in app timezone. */
export function formatBerlinDateTimeShort(value) {
const full = formatBerlinDateTime(value);
return full ? full.slice(0, 16) : '';
}