サンプル / タイムゾーン
タイムゾーンをまたぐ会議調整
ローカルのタイムゾーンで明日の会議時刻を選び、同じ瞬間を世界各地の都市の現地時刻で一覧表示します。
試してみよう
- 開始時刻を 23:00 に設定し、東京とコルカタが翌日に繰り上がる様子を確認します。
- プレビューバーで別のタイムゾーンに切り替え、その都市を基準に会議を設定した場合の表示を確認します。
cities.jsのCITIESに'Australia/Sydney'を追加し、一覧に都市が加わることを確認します。
import { tomorrow } from 'native-dates';
import { CITIES } from './cities.js';
const input = document.querySelector('input');
const summary = document.querySelector('#summary');
const list = document.querySelector('ul');
function render() {
const meeting = tomorrow().at(input.value);
summary.textContent = meeting.format('full');
list.innerHTML = CITIES.map((zone) => {
const there = meeting.in(zone);
const sameDay = there.date.equals(meeting.date);
return `<li>
<code>${zone}</code>
<b>${there.format('time')}</b>
<span class="muted">${sameDay ? '' : there.format({ weekday: 'long' })}</span>
</li>`;
}).join('');
}
input.addEventListener('input', render);
render();
起動中…
仕組み
会議の日時は tomorrow().at(input.value) で組み立てます。明日のカレンダー日付と選択した時刻を組み合わせた1つの瞬間を基準にするため、手動の UTC オフセット計算は不要です。
各都市については、meeting.in(zone) で同じ瞬間をその IANA タイムゾーンから参照し、format('time') で現地時刻を出力します。
there.date と開催側のカレンダー日付を比較することで、日付をまたいだ都市を判定できます。日付が異なる行だけに曜日ラベルが表示されます。