Public
Edited
Mar 11, 2023
Importers
Insert cell
Insert cell
function generateDates(startDate, endDate, isLocal = true) {
// Parse the start and end dates using D3's timeParse function
const parseTime = d3.timeParse("%Y-%m-%d");
const start = parseTime(startDate);
const end = parseTime(endDate);

// Create an array to store the generated dates
const dates = [];

// Loop through each day between the start and end dates, adding 1 day each time
for (let day = start; day <= end; day = d3.timeDay.offset(day, 1)) {
// Format the date as a string using D3's timeFormat function
const formattedDate = d3.timeFormat("%Y-%m-%d")(day);
const ts = isLocal
? Date.parse(`${formattedDate} 00:00:00`) / 1000
: Date.parse(`${formattedDate} 00:00:00 UTC`) / 1000;

// Add the formatted date to the dates array
dates.push({ ts, date: formattedDate });
}

// Return the generated dates array
return dates;
}
Insert cell
sampleGenerateDates = generateDates("2023-01-01", "2023-01-05")
Insert cell
typeof sampleGenerateDates[0].date
Insert cell
epoch2human = (ts, short = false) =>
short ? new Date(ts * 1000).toDateString() : new Date(ts * 1000).toString()
Insert cell
epoch2human(1672498800)
Insert cell
epoch2human(sampleGenerateDates[0].ts)
Insert cell
human2epoch = (date) => Math.floor(Date.parse(date) / 1000)
Insert cell
human2epoch("2023-01-01 00:00:00 GMT +9")
Insert cell
epochOffset = (ts, days) => ts + days * 86400 // 1 day in seconds is 24 * 60 * 60
Insert cell
epoch2human(epochOffset(1672498800, -1))
Insert cell
function timeCounter(t, obj = true) {
const days = parseInt(t / 86400);
t = t - days * 86400;

const hours = parseInt(t / 3600);
t = t - hours * 3600;

const minutes = parseInt(t / 60);
t = t - minutes * 60;

return obj
? { days, hours, minutes }
: `${days} days, ${hours} hours, and ${minutes} minutes`;
}
Insert cell
timeCounter(14 * 60 * 60)
Insert cell
function timeSinceMidnight(timestampInSeconds) {
// Convert the timestamp to milliseconds
const timestampInMs = timestampInSeconds * 1000;

// Create a new date object from the timestamp
const date = new Date(timestampInMs);

// Get the number of milliseconds elapsed since midnight
const elapsedMs =
date.getTime() -
new Date(date.getFullYear(), date.getMonth(), date.getDate()).getTime();

// Convert milliseconds to seconds and return the result
return Math.floor(elapsedMs / 1000);
}
Insert cell
function timeResetToMidnight(timestampInSeconds) {
// Convert the timestamp to milliseconds
const timestampInMs = timestampInSeconds * 1000;

// Create a new date object from the timestamp
const date = new Date(timestampInMs);

// Get the number of milliseconds elapsed since midnight
const elapsedMs =
date.getTime() -
new Date(date.getFullYear(), date.getMonth(), date.getDate()).getTime();

// Convert milliseconds to seconds and return the result
return timestampInSeconds - Math.floor(elapsedMs / 1000);
}
Insert cell
function timeLeftTillMidnight(timestampInSeconds) {
// Convert the timestamp to milliseconds
const timestampInMs = timestampInSeconds * 1000;

// Create a new date object from the timestamp
const date = new Date(timestampInMs);

// Get the timestamp for midnight of the same day
const midnightTimestampInMs = new Date(
date.getFullYear(),
date.getMonth(),
date.getDate() + 1
).getTime();

// Calculate the number of seconds left until midnight
const secondsLeft = Math.floor(
(midnightTimestampInMs - timestampInMs) / 1000
);

// Return the result
return secondsLeft;
}
Insert cell
async function getPublicHolidays(countries, year) {
const baseUrl = "https://date.nager.at/api/v3";

// Create an array to store the public holidays for all countries
const publicHolidays = [];

// Loop through each country and get its public holidays for the specified year
for (const country of countries) {
const url = `${baseUrl}/publicholidays/${year}/${country}`;

// Send a GET request to the API to get the public holidays for the country and year
const response = await fetch(url);
const holidays = await response.json();

// Add the public holidays for the country to the publicHolidays array
publicHolidays.push({
country,
holidays
});
}

// Return the public holidays for all countries
return publicHolidays;
}
Insert cell
Insert cell
note = {
const scope = DOM.uid("note").id;
const style = document.head.appendChild(htl.html`<style>
.${scope} {
border: 1px solid rgba(0, 0, 0, 0.05);
padding: 0.8rem;
max-width: 640px;
border-radius: 4px;
font: 13px/1.5em var(--sans-serif);
color: #444;
box-sizing: border-box;
background: hsl(55deg 80% 98%);
}
.${scope} code {
font-size: inherit;
}
`);
invalidation.then(() => style.remove());
return (contents) => htl.html`<div class="${scope}">${contents}</div>`;
}
Insert cell
function details(summary, content, open = false, className) {
return html`
<details ${open ? "open" : ""} ${className ? `class="${className}"` : ""}>
<summary>${summary}</summary>
${content}
</details>
`;
}
Insert cell
Insert cell
function card(content) {
return html`
<p class="card">
${content}
</div>
`;
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
2023-japan-holidays.csv
Type Table, then Shift-Enter. Ctrl-space for more options.

Insert cell
Insert cell
2023-china-holidays.csv
Type Table, then Shift-Enter. Ctrl-space for more options.

Insert cell
Insert cell
2023-south-korea-holidays.csv
Type Table, then Shift-Enter. Ctrl-space for more options.

Insert cell
Insert cell
2023-hong-kong-holidays.csv
Type Table, then Shift-Enter. Ctrl-space for more options.

Insert cell
Insert cell
2023-taiwan-holidays.csv
Type Table, then Shift-Enter. Ctrl-space for more options.

Insert cell
Insert cell
2023-macau-holidays.csv
Type Table, then Shift-Enter. Ctrl-space for more options.

Insert cell
Insert cell
2023-us-holidays.csv
Type Table, then Shift-Enter. Ctrl-space for more options.

Insert cell
Insert cell
2023-uk-holidays.csv
Type Table, then Shift-Enter. Ctrl-space for more options.

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