Examples / Localization
Japanese eras, dates, and shifts
Format Japanese imperial eras with gannen on year one, unpadded kanji dates, and overnight shifts across midnight.
Try this
- Click 平成最後の日 and 令和初日 to watch May 1, 2019 step from
平成31年4月30日to令和元年5月1日. - Pick 2026-05-01 to see the imperial calendar advance to
令和8年while keeping unpadded month and day numbers. - Edit the shift times across midnight and notice
timeRange()keeps a pure clock span with Japan's~wave-dash.
import { configure, date, timeRange } from 'native-dates';
configure({ locale: 'ja' });
const dayInput = document.querySelector('#day');
const start = document.querySelector('#start');
const end = document.querySelector('#end');
const output = document.querySelector('#output');
function render() {
if (!dayInput.value || !start.value || !end.value) { return; }
const day = date(dayInput.value);
const span = day.to(day.plus(4, 'days'));
const shift = timeRange(start.value, end.value);
const rows = [
['Full date', ".format('full')", day.format('full')],
['Month heading', '.yearMonth.text', day.yearMonth.text],
['Date span', '.text', span.text],
['Overnight shift', '.text', shift.text],
['Imperial era', 'ja-u-ca-japanese', day.format('long', 'ja-u-ca-japanese')],
];
output.innerHTML = rows.map(([label, locale, value]) => `
<li>
<span class="label">${label} <code>${locale}</code></span>
<b>${value}</b>
</li>`).join('');
}
for (const button of document.querySelectorAll('[data-date]')) {
button.addEventListener('click', () => {
dayInput.value = button.dataset.date;
render();
});
}
dayInput.addEventListener('input', render);
start.addEventListener('input', render);
end.addEventListener('input', render);
render();
Starting…
How it works
Calling configure({ locale: 'ja' }) once sets Japanese across every text preview and format preset. Standard presets pair unpadded numbers with kanji counters, and day.yearMonth drops the day counter for monthly headings.
Date spans and overnight timeRange() values read that same configured locale automatically. Both join their endpoints with the Japanese wave-dash without injecting artificial calendar dates across midnight.
Pass a locale tag only when opting into another calendar. Passing 'ja-u-ca-japanese' formats on the Japanese imperial calendar and prints 元年 on the first year of each era.