UNPKG

easy-dates

Version:

A tiny, zero-dependency utility library for working with static dates.

490 lines (455 loc) 12.4 kB
// src/closestTo/closestTo.js function closestTo(controlDate, args) { const cleanControlDate = Math.floor(new Date(controlDate).getTime()); let closest; let closestIndex; let difference = 0; function assignValues(localDiff, closestDate, closestIdx) { difference = localDiff; closest = closestDate; closestIndex = closestIdx; } for (let i = 0; i < args.length; i++) { const localItem = Math.floor(new Date(args[i]).getTime()); const localDifference = Math.abs(cleanControlDate - localItem); if (difference === 0) { assignValues(localDifference, args[i], i); } else { if (difference > localDifference) { assignValues(localDifference, args[i], i); } } } return { difference, closest, closestIndex }; } // src/dateNow/dateNow.js function dateNow(locale) { let output = String(); if (locale) { output = new Date(Date.now()).toLocaleString(locale); } else { output = new Date(Date.now()).toLocaleString("en-CA"); } return output; } // src/dateToUnix/dateToUnix.js function dateToUnix(date) { if (typeof date === "string") { const formattedDate = new Date(date.split(",")[0]).getTime() + 8694e4 / 1e3; if (new Date(formattedDate).toString() !== "Invalid Date") { return formattedDate; } else { throw new Error("dateToUnix: Invalid Date string"); } } else if (typeof date === "number") { return Math.floor(date / 1e3); } else if (typeof date === "object") { return Math.floor(date.getTime() / 1e3); } else { throw new Error("dateToUnix: date must be a string or number or Date object"); } } // src/daysFromNow/daysFromNow.js function daysFromNow(multiplier, locale) { const twentyFourHours = Number(864e5); let output; if (locale?.length > 0) { output = new Date(Date.now() + Number(multiplier) * Number(twentyFourHours)).toLocaleString(locale); } else { output = new Date(Date.now() + Number(multiplier) * Number(twentyFourHours)); } return output; } // src/getDaysInMonth/getDaysInMonth.js function getDaysInMonth(monthIndex, year) { let month = Number(); const monthObj = { jan: 0, feb: 1, mar: 2, apr: 3, may: 4, jun: 5, jul: 6, aug: 7, sep: 8, oct: 9, nov: 10, dec: 11 }; if (typeof year === "string" || typeof monthIndex === "string" || !monthIndex && !year) { if (monthIndex?.toString().toLowerCase() === "current" || year?.toString().toLowerCase() === "current" || !monthIndex && !year) { !year ? year = new Date().getFullYear() : year; month = new Date().getMonth(); } else { let firstThree = String(); if (monthIndex.length > 3) { firstThree = monthIndex.toString().toLowerCase().slice(0, 3); } else { firstThree = monthIndex; } month = monthObj[firstThree]; } } else { month = Number(monthIndex); } return 32 - new Date(year, month, 32).getDate(); } // src/getDuration/getDuration.js function getDuration(firstDate, secondDate, measure) { const oneUnixYear = 31536e3; const firstDateUnix = dateToUnix(firstDate); const secondDateUnix = dateToUnix(secondDate); const difference = Math.abs(firstDateUnix - secondDateUnix); let output; switch (measure) { case "seconds": output = difference / oneUnixYear * 8760 * 60 * 60; break; case "minutes": output = difference / oneUnixYear * 8760 * 60; break; case "hours": output = difference / oneUnixYear * 8760; break; case "days": output = difference / oneUnixYear * 365; break; case "weeks": output = difference / oneUnixYear * 52; break; case "months": output = difference / oneUnixYear * 12; break; case "years": output = difference / oneUnixYear; break; default: output = difference / oneUnixYear * 365; } return output; } // src/getMonthIndex/getMonthIndex.js function getMonthIndex(monthName) { if (monthName === "current" || !monthName) { return new Date().getMonth(); } else { const fullMonths = [ "jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec" ]; return fullMonths.indexOf(monthName.slice(0, 3).toLowerCase()); } } // src/getTodayName/getTodayName.js function getTodayName() { const fullDays = [ "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" ]; return fullDays[new Date().getDay()].toLowerCase(); } // src/getTomorrow/getTomorrow.js function getTomorrow(locale) { const twentyFourHours = Number(864e5); if (locale) { return new Date(Date.now() + twentyFourHours).toLocaleString(locale); } else { return new Date(Date.now() + twentyFourHours).toLocaleString("en-CA"); } } // src/getYear/getYear.js function getYear() { return new Date().getFullYear(); } // src/unixToDuration/unixToDuration.js function unixToDuration(unixTimestamp, measure) { let output; switch (measure) { case "seconds": output = unixTimestamp; break; case "minutes": output = unixTimestamp / 60; break; case "hours": output = unixTimestamp / 3600; break; case "days": output = unixTimestamp / 86400; break; case "weeks": output = unixTimestamp / 604800; break; case "months": output = unixTimestamp / 2592e3; break; case "years": output = unixTimestamp / 31449600; break; case "decades": output = unixTimestamp / 314496e3; break; default: output = unixTimestamp / 86400; } return output; } // src/unixToDate/unixToDate.js function unixToDate(unixTimeStamp, locale) { let output; if (locale) { output = new Date(unixTimeStamp * 1e3).toLocaleString(locale); } else { output = new Date(unixTimeStamp * 1e3); } return output; } // src/isAfter/isAfter.js function isAfter(date, dateToCompare) { if (date instanceof Date && dateToCompare instanceof Date) { return date.getTime() > dateToCompare.getTime(); } else { throw new Error("Invalid arguments"); } } // src/isBefore/isBefore.js function isBefore(date, dateToCompare) { if (date instanceof Date && dateToCompare instanceof Date) { return date.getTime() < dateToCompare.getTime(); } else { throw new Error("Invalid arguments"); } } // src/isDate/isDate.js function isDate(date) { return date instanceof Date; } // src/isEqual/isEqual.js function isEqual(dateOne, DateTwo) { if (dateOne instanceof Date && DateTwo instanceof Date) { return dateOne.getTime() === DateTwo.getTime(); } else { throw new Error("Both arguments must be Date objects"); } } // src/isInFuture/isInFuture.js function isInFuture(date) { if (date instanceof Date) { return date.getTime() > Date.now(); } else { throw new Error("date must be a Date object"); } } // src/isInPast/isInPast.js function isInPast(date) { if (date instanceof Date) { return date.getTime() < Date.now(); } else { throw new Error("date must be a Date object"); } } // src/monthsToYears/monthsToYears.js function monthsToYears(months) { if (typeof months === "number") { return months / 12; } else { throw new Error("monthsToYears: months must be a number"); } } // src/daysToWeeks/daysToWeeks.js function daysToWeeks(days) { if (typeof days === "number") { return days / 7; } else { throw new Error("daysToWeeks: days must be a number"); } } // src/hoursToMilliseconds/hoursToMilliseconds.js function hoursToMilliseconds(hours) { if (typeof hours === "number" || typeof hours === "string" && Number.isInteger(Number(hours))) { return hours * 60 * 60 * 1e3; } else { throw new Error("hoursToMilliseconds: hours must be a number"); } } // src/hoursToMinutes/hoursToMinutes.js function hoursToMinutes(hours) { if (typeof hours === "number") { return hours * 60; } else { throw new Error("hoursToMinutes: hours must be a number"); } } // src/millisecondsToHours/millisecondsToHours.js function millisecondsToHours(milliseconds) { if (typeof milliseconds === "number") { return milliseconds / 1e3 / 60 / 60; } else { throw new Error("millisecondsToHours: milliseconds must be a number"); } } // src/millisecondsToMinutes/millisecondsToMinutes.js function millisecondsToMinutes(milliseconds) { if (typeof milliseconds === "number") { return milliseconds / 6e4; } else { throw new Error("millisecondsToMinutes: milliseconds must be a number"); } } // src/minutesToHours/minutesToHours.js function minutesToHours(minutes) { if (typeof minutes === "number") { return minutes / 60; } else { throw new Error("minutesToHours expects a number"); } } // src/minutesToMilliseconds/minutesToMilliseconds.js function minutesToMilliseconds(minutes) { if (typeof minutes === "number") { return minutes * 6e4; } else { throw new Error("minutesToMilliseconds: minutes must be a number"); } } // src/findLatest/findLatest.js function findLatest(arr) { if (arr.length <= 1) { throw new Error("findLatest: Array should contain at least 2 dates"); } else { return arr.reduce((acc, curr) => { if (typeof acc === "number" && typeof curr === "number") { return Math.max(acc, curr); } else if (typeof acc === "object" && typeof curr === "object") { return Math.max(acc.getTime(), curr.getTime()); } else { throw new Error("findLatest: Array should contain only numbers"); } }); } } // src/findEarliest/findEarliest.js function findEarliest(arr) { if (arr.length <= 1) { throw new Error("findEarliest: Array should contain at least 2 dates"); } else { return arr.reduce((acc, curr) => { if (typeof acc === "number" && typeof curr === "number") { return Math.max(acc, curr); } else if (typeof acc === "object" && typeof curr === "object") { return Math.max(acc.getTime(), curr.getTime()); } else { throw new Error("findEarliest: Array should contain only numbers"); } }); } } // src/minutesToSeconds/minutesToSeconds.js function minutesToSeconds(minutes) { if (typeof minutes === "number") { return minutes * 60; } else { throw new Error("minutesToSeconds: expects a number"); } } // src/monthsToQuarters/monthsToQuarters.js function monthsToQuarters(months) { if (typeof months === "number") { return months / 3; } else { throw new Error("monthsToQuarters: expects a number"); } } // src/quartersToMonths/quartersToMonths.js function quartersToMonths(quarters) { if (typeof quarters === "number") { return quarters * 3; } else { throw new Error("quartersToMonths: expects a number"); } } // src/quartersToYears/quartersToYears.js function quartersToYears(quarters) { if (typeof quarters === "number") { return quarters / 4; } else { throw new Error("quartersToYears: expects a number"); } } // src/secondsToMilliseconds/secondsToMilliseconds.js function secondsToMilliseconds(seconds) { if (typeof seconds === "number") { return seconds * 1e3; } else { throw new Error("secondsToMilliseconds: expects a number"); } } // src/secondsToMinutes/secondsToMinutes.js function secondsToMinutes(seconds) { if (typeof seconds === "number") { return seconds / 60; } else { throw new Error("secondsToMinutes: expects a number"); } } // src/secondsToHours/secondsToHours.js function secondsToHours(seconds) { if (typeof seconds === "number") { return seconds / 3600; } else { throw new Error("secondsToHours: expects a number"); } } export { closestTo, dateNow, dateToUnix, daysFromNow, daysToWeeks, findEarliest, findLatest, getDaysInMonth, getDuration, getMonthIndex, getTodayName, getTomorrow, getYear, hoursToMilliseconds, hoursToMinutes, isAfter, isBefore, isDate, isEqual, isInFuture, isInPast, millisecondsToHours, millisecondsToMinutes, minutesToHours, minutesToMilliseconds, minutesToSeconds, monthsToQuarters, monthsToYears, quartersToMonths, quartersToYears, secondsToHours, secondsToMilliseconds, secondsToMinutes, unixToDate, unixToDuration };