예제 / 현지화
일본어 연호, 날짜, 야간 근무 구간
첫해에 원년(元年)을 쓰는 일본 연호, 앞 0을 채우지 않는 한자 날짜, 자정을 넘기는 야간 근무 시각 범위를 포맷합니다.
직접 해 보기
- 平成最後の日와 令和初日를 차례로 클릭해 2019년 5월 1일을 기점으로
平成31年4月30日에서令和元年5月1日로 전환되는 모습을 확인해 보세요. - 2026-05-01을 선택해 연호가
令和8年으로 올라가면서도 월과 일 숫자에 불필요한 0을 채우지 않는지 살펴보세요. - 자정을 넘기도록 근무 시각을 수정해 보고
timeRange()가 일본식 물결표~로 순수한 시각 범위만 유지하는지 확인해 보세요.
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();
시작하는 중…
동작 원리
configure({ locale: 'ja' })를 한 번 호출하면 모든 텍스트 미리보기와 포맷 프리셋에 일본어가 적용됩니다. 기본 프리셋은 앞 0이 없는 숫자와 한자 단위를 조합하며, day.yearMonth는 월간 제목용으로 일 단위를 떼어냅니다.
날짜 범위와 자정을 넘기는 timeRange() 값도 설정된 로케일을 자동으로 따릅니다. 두 범위 모두 자정 전후에 임의의 달력 날짜를 끼워 넣지 않고 일본식 물결표로 시작과 끝을 연결합니다.
다른 역법을 선택할 때만 로케일 태그를 전달하세요. 'ja-u-ca-japanese'를 전달하면 일본 연호력으로 포맷하며 각 연호의 첫해는 元年으로 출력합니다.