Examples / Localization
Portuguese crasis, ranges, and day-first input
Format Brazilian Portuguese moments with hyphenated weekdays and às crasis before the hour, collapse date ranges, and read day-first input.
Try this
- Pick 2026-05-17 in Date to switch from hyphenated sexta-feira to single-word domingo while keeping
àsbefore the hour. - Set Days to 20 so the range crosses from May into June and prints both
de maioandde junho. - Edit Typed date to
25/12/2026orem breveto compare day-first parsing against Unreadable input.
import '@native-dates/plugins/loose';
import { configure, date } from 'native-dates';
configure({ locale: 'pt-BR' });
const day = document.querySelector('#day');
const clock = document.querySelector('#clock');
const span = document.querySelector('#span');
const typed = document.querySelector('#typed');
const output = document.querySelector('#output');
function render() {
if (!day.value || !clock.value || span.value === '') {
return;
}
const start = date(day.value);
const moment = start.at(clock.value, 'America/Sao_Paulo');
const range = start.to(start.plus(Number(span.value), 'days'));
const parsed = date(typed.value, { loose: true });
output.innerHTML = `
<dt>Date and clock <code>.format('full')</code></dt>
<dd>${moment.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?.format('long') ?? 'Unreadable'}</dd>`;
}
for (const control of [day, clock, span, typed]) {
control.addEventListener('input', render);
}
render();
Starting…
How it works
Setting 'pt-BR' once in configure() applies Brazilian Portuguese formatting and day-first loose parsing everywhere. Formatting a zoned moment preserves hyphenated weekdays like “sexta-feira”, lowercase months, and the crasis “às” before the hour.
Multi-day spans collapse shared month and year fields through range.format('long'). Staying within May prints “de maio de 2026” once at the end instead of repeating it on both days.
Reading start.yearMonth isolates the billing or calendar month and keeps the “de” preposition in “maio de 2026”.
Because 'pt-BR' writes dates day-first, date(typed.value, { loose: true }) reads “08/05/2026” as May 8 and returns null when the text is invalid.