Compare two dates
Compare validated ISO calendar dates in UTC and return -1, 0 or 1.
function compareDates({ first, second }) { return Math.sign(new Date(first).getTime() - new Dat…date & timeCollection · 24 utilities
Use explicit ISO calendar dates and avoid ambiguous date parsing and hidden timezone assumptions.
Compare validated ISO calendar dates in UTC and return -1, 0 or 1.
function compareDates({ first, second }) { return Math.sign(new Date(first).getTime() - new Dat…date & timeTurn an hour from 0 to 23 into a 12-hour label with am or pm.
function suffixAmPm(hour) { return `${hour % 12 === 0 ? 12 : hour % 12}${hour < 12 ? 'am' : 'pm…date & timeCount whole calendar days between two YYYY-MM-DD dates.
function diffDays({ first, second }) { return Math.round(Math.abs(Date.parse(second) - Date.par…date & timeFormat an ISO date-time as its UTC calendar date.
function formatYmd(dateTime) { return new Date(dateTime).toISOString().slice(0, 10); }date & timeFormat a whole number of seconds as zero-padded hours, minutes and seconds.
function formatSeconds(seconds) { return [Math.floor(seconds / 3600), Math.floor(seconds % 3600…date & timeCount calendar month boundaries from start to end.
function monthDiff({ start, end }) { const a = new Date(start), b = new Date(end); return Math.…date & timeSplit the UTC ISO string into its numeric parts.
function extract(dateTime) { return new Date(dateTime).toISOString().split(/[^0-9]/).slice(0, -…date & timeConvert a date-time to whole Unix seconds.
function unixSeconds(now) { return Math.floor(Date.parse(now) / 1000); }date & timeSet the UTC day of the month to 1.
function firstDateOfMonth(date) { const d = new Date(date); d.setUTCDate(1); return d.toISOStri…date & timeReturn 366 for Gregorian leap years and 365 otherwise.
function daysInYear(year) { return (year % 4 === 0 && year % 100 !== 0) || year % 400 === 0 ? 3…date & timeReturn the start of the UTC day of a date-time.
function midnightUtc(now) { const d = new Date(now); d.setUTCHours(0, 0, 0, 0); return d.toISOS…date & timeUse Intl.DateTimeFormat with an explicit locale and time zone.
function formatDate({ date, locale }) { return new Intl.DateTimeFormat(locale, { timeZone: 'UTC…date & timeMap the month to a quarter from 1 to 4.
function quarter(date) { return Math.ceil((new Date(date).getUTCMonth() + 1) / 3); }date & timeCount days from January 1, which is day 1.
function dayOfYear(date) { return Math.round((Date.parse(date) - Date.parse(`${date.slice(0, 4)…date & timeStep to day 0 of the next month in UTC.
function lastDateOfMonth(date) { const d = new Date(date); d.setUTCMonth(d.getUTCMonth() + 1, 0…date & timeLook up the English month name of a date.
function monthName(date) { return ['January', 'February', 'March', 'April', 'May', 'June', 'Jul…date & timeAdd one calendar day in UTC.
function tomorrow(date) { const d = new Date(date); d.setUTCDate(d.getUTCDate() + 1); return d.…date & timeLook up the English weekday name of a date.
function weekday(date) { return ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Frida…date & timeSort dates ascending or descending without changing the input.
function sortDates({ dates, order = 'asc' }) { const sorted = [...dates].sort((a, b) => Date.pa…date & timeAdd one to the UTC year.
function plusOneYear(date) { const d = new Date(date); d.setUTCFullYear(d.getUTCFullYear() + 1)…date & timeRead day 0 of the next month for a 1-based month.
function daysInMonth({ year, month }) { const d = new Date(0); d.setUTCFullYear(year, month, 0)…date & timeSubtract one calendar day in UTC.
function yesterday(date) { const d = new Date(date); d.setUTCDate(d.getUTCDate() - 1); return d…date & timeSplit decimal hours into whole hours and whole minutes.
function hoursAndMinutes(value) { return [Math.floor(value), Math.floor((value * 60) % 60)]; }date & timeRead the runtime’s IANA time zone, such as America/Sao_Paulo.
function getTimezone() { return Intl.DateTimeFormat().resolvedOptions().timeZone; }date & timeTry a function in the local code lab, then read its limits before using it in an application.