Examples / Time zones
Open right now
Check whether venues across time zones are open at any clock time, including overnight hours that cross midnight.
Try this
- Pick
Asia/Tokyoin the preview bar and set the time to 01:00 to see the ramen bar stay open past midnight. - Switch the preview bar to
Europe/LondonorAmerica/Los_Angelesto watch the same clock shift around the world. - Add a venue with overnight hours like
22:00to04:00inPLACESinsideapp.js.
import { now, timeRange, today } from 'native-dates';
const PLACES = [
{ name: 'San Francisco coffee bar', zone: 'America/Los_Angeles', open: '06:00', close: '16:00' },
{ name: 'New York diner', zone: 'America/New_York', open: '07:00', close: '23:00' },
{ name: 'London pub', zone: 'Europe/London', open: '12:00', close: '00:30' },
{ name: 'Berlin bakery', zone: 'Europe/Berlin', open: '06:30', close: '15:00' },
{ name: 'Tokyo ramen bar', zone: 'Asia/Tokyo', open: '18:00', close: '02:00' },
];
const input = document.querySelector('input');
const list = document.querySelector('ul');
input.value = now().format('HH:mm');
function render() {
const moment = today().at(input.value);
list.innerHTML = PLACES.map((place) => {
const local = moment.in(place.zone);
const hours = timeRange(place.open, place.close);
const isOpen = hours.contains(local.time);
return `<li>
<div>
<b>${place.name}</b>
<span class="muted">${hours.format()}</span>
</div>
<span class="clock">${local.format('time')}</span>
<span class="badge ${isOpen ? 'open' : 'closed'}">${isOpen ? 'Open' : 'Closed'}</span>
</li>`;
}).join('');
}
input.addEventListener('input', render);
render();
Starting…
How it works
Each venue defines its schedule as a timeRange(open, close) in its own local clock. When closing falls earlier than opening, the range wraps across midnight automatically.
For any chosen time, today().at(input.value) builds the moment and in(place.zone) converts it to the venue’s local clock.
Checking hours.contains(local.time) then answers whether the venue is open, without special-casing overnight spans or manual UTC math.