UNPKG

isoformat

Version:

A tidy ISO 8601 date formatter and parser

26 lines (23 loc) 910 B
export default function format(date, fallback) { if (!(date instanceof Date)) date = new Date(+date); if (isNaN(date)) return typeof fallback === "function" ? fallback(date) : fallback; const hours = date.getUTCHours(); const minutes = date.getUTCMinutes(); const seconds = date.getUTCSeconds(); const milliseconds = date.getUTCMilliseconds(); return `${formatYear(date.getUTCFullYear(), 4)}-${pad(date.getUTCMonth() + 1, 2)}-${pad(date.getUTCDate(), 2)}${ hours || minutes || seconds || milliseconds ? `T${pad(hours, 2)}:${pad(minutes, 2)}${ seconds || milliseconds ? `:${pad(seconds, 2)}${ milliseconds ? `.${pad(milliseconds, 3)}` : `` }` : `` }Z` : `` }`; } function formatYear(year) { return year < 0 ? `-${pad(-year, 6)}` : year > 9999 ? `+${pad(year, 6)}` : pad(year, 4); } function pad(value, width) { return `${value}`.padStart(width, "0"); }