Examples / Lengths of time
Countdown to a launch
The time left until Friday at 5 PM, as a duration you can print three ways. until() does the subtraction and keeps the units.
Try this
- Watch the seconds. Each tick asks
now().until(launch)again and prints what comes back. - Change
'friday'to'monday'inapp.js. The launch moves, and so does the count. - Pick
Asia/Tokyoin the preview’s bar. Friday at 5 PM comes sooner there, so less time is left.
import { now, today } from 'native-dates';
const launch = today().next('friday').at('5pm');
document.querySelector('#launch').textContent = launch.format('full');
function tick() {
const left = now().until(launch).round('second');
document.querySelector('#long').textContent = left.format();
document.querySelector('#narrow').textContent = left.format('narrow');
document.querySelector('#digital').textContent = left.format('digital');
}
setInterval(tick, 1000);
tick();
Starting…
How it works
today().next('friday').at('5pm') reads as the sentence it is: the next Friday after today, at five
in the afternoon, here. until() returns a Duration whose parts are already balanced into days,
hours, minutes and seconds, so there is no dividing by 86,400,000.
format() prints a duration in the reader’s language. 'narrow' and 'digital' are the same
duration in tighter forms.