UNPKG

utilite

Version:

Powerful utility library for JS

93 lines (92 loc) 3.29 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.customDateFormat = exports.formatToTimeAgo = exports.formatTime = exports.formatToLongDate = exports.formatToMMDDYYYY = exports.formatToISO = exports.getDaysInMonth = exports.isLeapYear = exports.getDayOfWeek = exports.addDaysToDate = exports.getCurrentDate = exports.formatDate = void 0; function formatDate(date, format) { const options = { year: "numeric", month: "2-digit", day: "2-digit", }; return date.toLocaleDateString(undefined, options); } exports.formatDate = formatDate; function getCurrentDate() { return new Date(); } exports.getCurrentDate = getCurrentDate; function addDaysToDate(date, days) { const result = new Date(date); result.setDate(result.getDate() + days); return result; } exports.addDaysToDate = addDaysToDate; function getDayOfWeek(date) { const options = { weekday: "long" }; return date.toLocaleDateString(undefined, options); } exports.getDayOfWeek = getDayOfWeek; function isLeapYear(year) { return (year % 4 === 0 && year % 100 !== 0) || year % 400 === 0; } exports.isLeapYear = isLeapYear; function getDaysInMonth(year, month) { return new Date(year, month + 1, 0).getDate(); } exports.getDaysInMonth = getDaysInMonth; function formatToISO(date) { return date.toISOString(); } exports.formatToISO = formatToISO; function formatToMMDDYYYY(date) { const month = String(date.getMonth() + 1).padStart(2, "0"); const day = String(date.getDate()).padStart(2, "0"); const year = date.getFullYear(); return `${month}/${day}/${year}`; } exports.formatToMMDDYYYY = formatToMMDDYYYY; function formatToLongDate(date) { const options = { weekday: "long", year: "numeric", month: "long", day: "numeric", }; return date.toLocaleDateString(undefined, options); } exports.formatToLongDate = formatToLongDate; function formatTime(date) { const options = { hour: "2-digit", minute: "2-digit", }; return date.toLocaleTimeString(undefined, options); } exports.formatTime = formatTime; function formatToTimeAgo(date) { const now = new Date(); const diff = now.getTime() - date.getTime(); const seconds = Math.floor(diff / 1000); const minutes = Math.floor(seconds / 60); const hours = Math.floor(minutes / 60); const days = Math.floor(hours / 24); if (days > 0) return `${days} day${days === 1 ? "" : "s"} ago`; if (hours > 0) return `${hours} hour${hours === 1 ? "" : "s"} ago`; if (minutes > 0) return `${minutes} minute${minutes === 1 ? "" : "s"} ago`; return `${seconds} second${seconds === 1 ? "" : "s"} ago`; } exports.formatToTimeAgo = formatToTimeAgo; function customDateFormat(date, format) { const map = { MM: String(date.getMonth() + 1).padStart(2, "0"), DD: String(date.getDate()).padStart(2, "0"), YYYY: String(date.getFullYear()), HH: String(date.getHours()).padStart(2, "0"), mm: String(date.getMinutes()).padStart(2, "0"), ss: String(date.getSeconds()).padStart(2, "0"), }; return format.replace(/MM|DD|YYYY|HH|mm|ss/g, (match) => map[match]); } exports.customDateFormat = customDateFormat;