Published
Edited
Jul 9, 2021
Fork of Countdown
Importers
1 star
Insert cell
Insert cell
Insert cell
function interval(then, now) {
if (then <= now) throw new Error("Time’s up!");
let t = now;
let years = d3.timeYear.count(now, then);
if ((t = d3.timeYear.offset(t, years)) > then) t = d3.timeYear.offset(t, -1), --years;
let months = d3.timeMonth.count(t, then);
if ((t = d3.timeMonth.offset(t, months)) > then) t = d3.timeMonth.offset(t, -1), --months;
let days = d3.timeDay.count(t, then);
if ((t = d3.timeDay.offset(t, days)) > then) t = d3.timeDay.offset(t, -1), --days;
let hours = d3.timeHour.count(t, then);
if ((t = d3.timeHour.offset(t, hours)) > then) t = d3.timeHour.offset(t, -1), --hours;
let minutes = d3.timeMinute.count(t, then);
if ((t = d3.timeMinute.offset(t, minutes)) > then) t = d3.timeMinute.offset(t, -1), --minutes;
let seconds = d3.timeSecond.count(t, then);
if ((t = d3.timeSecond.offset(t, seconds)) > then) t = d3.timeSecond.offset(t, -1), --seconds;
return {years, months, days, hours, minutes, seconds};
}
Insert cell
function formatInterval(interval) {
let format = [];
for (const key in interval) {
if (interval[key] === 1) {
format.push(`${interval[key]} ${key.slice(0, -1)}`);
} else if (format.length || interval[key] > 1) {
format.push(`${interval[key]} ${key}`);
}
}
if (format.length > 1) {
format[format.length - 1] = `and ${format[format.length - 1]}`;
}
return format.join(",\n");
}
Insert cell
then = new Date(Date.UTC(2063, 3, 5, 17))
Insert cell
now = {
while (true) {
const date = new Date(Math.ceil((Date.now() + 1) / 1000) * 1000);
yield Promises.when(date, date);
}
}
Insert cell
d3 = require("d3-time@2")
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more