Collection · 24 utilities

Date & time utilities

Use explicit ISO calendar dates and avoid ambiguous date parsing and hidden timezone assumptions.

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 & time

Add AM PM suffix to an hour

Turn 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 & time

Convert a date to YYYY-MM-DD format

Format an ISO date-time as its UTC calendar date.

function formatYmd(dateTime) { return new Date(dateTime).toISOString().slice(0, 10); }date & time

Convert seconds to hh:mm:ss format

Format 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 & time

Get the total number of days in a year

Return 366 for Gregorian leap years and 365 otherwise.

function daysInYear(year) { return (year % 4 === 0 && year % 100 !== 0) || year % 400 === 0 ? 3…date & time

Set the time of a date to midnight

Return 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 & time

Format a date for the given locale

Use Intl.DateTimeFormat with an explicit locale and time zone.

function formatDate({ date, locale }) { return new Intl.DateTimeFormat(locale, { timeZone: 'UTC…date & time

Get the current quarter of a date

Map the month to a quarter from 1 to 4.

function quarter(date) { return Math.ceil((new Date(date).getUTCMonth() + 1) / 3); }date & time

Get the day of the year from a date

Count days from January 1, which is day 1.

function dayOfYear(date) { return Math.round((Date.parse(date) - Date.parse(`${date.slice(0, 4)…date & time

Get the month name of a date

Look up the English month name of a date.

function monthName(date) { return ['January', 'February', 'March', 'April', 'May', 'June', 'Jul…date & time

Get the tomorrow date

Add one calendar day in UTC.

function tomorrow(date) { const d = new Date(date); d.setUTCDate(d.getUTCDate() + 1); return d.…date & time

Get the weekday of a date

Look up the English weekday name of a date.

function weekday(date) { return ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Frida…date & time

Sort an array of dates

Sort dates ascending or descending without changing the input.

function sortDates({ dates, order = 'asc' }) { const sorted = [...dates].sort((a, b) => Date.pa…date & time

Determine one year from a date

Add one to the UTC year.

function plusOneYear(date) { const d = new Date(date); d.setUTCFullYear(d.getUTCFullYear() + 1)…date & time

Get the number of days in a given month

Read 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 & time

Get the yesterday date

Subtract one calendar day in UTC.

function yesterday(date) { const d = new Date(date); d.setUTCDate(d.getUTCDate() - 1); return d…date & time

Get hours and minutes from decimal time

Split decimal hours into whole hours and whole minutes.

function hoursAndMinutes(value) { return [Math.floor(value), Math.floor((value * 60) % 60)]; }date & time

Get the timezone string

Read the runtime’s IANA time zone, such as America/Sao_Paulo.

function getTimezone() { return Intl.DateTimeFormat().resolvedOptions().timeZone; }date & time

Try a function in the local code lab, then read its limits before using it in an application.

Type two or more characters. Try “duplicate”, “webhook” or “retry”.