Calculate the number of days to wait before Christmas is here.
export const take = [{ type: "date" }]
export const make = (eventDate) => {
if (!eventDate) return
const todayDate = new Date()
const dayMs = 1000 * 60 * 60 * 24
const delta = Math.abs(Math.round((eventDate - todayDate) / dayMs))
const days = new Intl.NumberFormat().format(delta)
const date = new Intl.DateTimeFormat(undefined, {
year: "numeric",
day: "numeric",
month: "long",
weekday: "long",
era: eventDate.getFullYear() < 1200 ? "short" : undefined,
}).format(eventDate)
const isOrAre = delta === 1 ? "is" : "are"
const dayOrDays = delta === 1 ? "day" : "days"
let value, detail
if (delta === 0) {
value = `It’s here!`
detail = `It’s ${date} today!`
} else if (eventDate - todayDate > 0) {
value = `${days} ${dayOrDays} left`
detail = `There ${isOrAre} ${days} ${dayOrDays} to go until ${date}!`
} else {
value = `${days} ${dayOrDays} ago`
detail = `It’s been ${days} ${dayOrDays} since ${date}!`
}
return {
type: "card",
value,
detail,
}
}