Examples / Localization
Spanish connectors, casing, and day-first dates
Format Spanish dates, collapsed ranges, and month headings, and parse day-first slash strings without month-first mixups.
Try this
- Set Days to 35 so the range crosses into June and repeats
de mayoandde junioon each side. - Type
15/08/2026into Typed date to read the day before the month and format 15 de agosto de 2026. - Type
32/05/2026orpendienteinto Typed date to see loose mode returnnulland show Unreadable.
import '@native-dates/plugins/loose';
import { configure, date } from 'native-dates';
configure({ locale: 'es' });
const day = document.querySelector('#day');
const span = document.querySelector('#span');
const typed = document.querySelector('#typed');
const output = document.querySelector('#output');
function render() {
if (!day.value || span.value === '') {
return;
}
const start = date(day.value);
const range = start.to(start.plus(Number(span.value), 'days'));
const parsed = date(typed.value, { loose: true });
output.innerHTML = `
<dt>Full date <code>.format('full')</code></dt>
<dd>${start.format('full')}</dd>
<dt>Collapsed range <code>.format('long')</code></dt>
<dd>${range.format('long')}</dd>
<dt>Month heading <code>.yearMonth</code></dt>
<dd>${start.yearMonth.format('long')}</dd>
<dt>Day-first input <code>{ loose: true }</code></dt>
<dd>${parsed ? parsed.format('long') : 'Unreadable'}</dd>`;
}
for (const control of [day, span, typed]) {
control.addEventListener('input', render);
}
render();
Starting…
How it works
Setting 'es' once in configure() applies Spanish formatting and day-first loose parsing across your whole app. Full dates keep weekday and month names lowercase and link day, month, and year with “de”.
Date ranges collapse shared parts through range.format('long') without repeating the locale. A span inside May keeps “de mayo de 2026” once, while crossing into June prints each month name around the dash.
Calendar headers read start.yearMonth to format “mayo de 2026” with its “de” connector intact.
Because 'es' writes dates day-first, date(typed.value, { loose: true }) reads “01/05/2026” as May 1 and returns null on invalid text.