UNPKG

date-fran

Version:

Date functionalities for javascript projects.

304 lines (303 loc) 15.2 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.todaysFormDate = exports.tomorrowsFormDate = exports.yesterdaysFormDate = exports.dateConstrainer = exports.dateForwardBuild = exports.differenceInDays = exports.todaysDate = exports.dateAcronym = exports.getMonth = exports.givenDateAndCurrentTime = exports.tomorrowsDate = exports.yesterdaysDate = exports.currentHourString = exports.actualHour = exports.monthAndIndex = exports.monthAndNumberOfDays = exports.indexOfThrityOneDaysMonth = exports.indexOfThrityDaysMonth = void 0; const currentYear = new Date().getFullYear(); const currentMonth = new Date().getMonth(); const dateOfTheMonth = new Date().getDate(); exports.indexOfThrityDaysMonth = [3, 5, 8, 10]; exports.indexOfThrityOneDaysMonth = [0, 2, 4, 6, 7, 9, 11]; exports.monthAndNumberOfDays = { "Jan": 31, "Feb": currentYear % 4 === 0 ? 29 : 28, "Mar": 31, "Apr": 30, "May": 31, "Jun": 30, "Jul": 31, "Aug": 31, "Sep": 30, "Oct": 31, "Nov": 30, "Dec": 31, }; exports.monthAndIndex = { 0: "Jan", 1: "Feb", 2: "Mar", 3: "Apr", 4: "May", 5: "Jun", 6: "Jul", 7: "Aug", 8: "Sep", 9: "Oct", 10: "Nov", 11: "Dec", }; const actualHour = (time) => { if (time[0] === "12") { if (time[2].includes("AM")) { return 1; } else { return 12 + 1; } } else if (time[2].includes("PM")) { return (+time[0] + 1) + 12; } else { return +time[0] + 1; } }; exports.actualHour = actualHour; const currentHourString = () => { // console.log("\n\t current Hour String: ",new Date().toLocaleTimeString(), new Date().toLocaleTimeString().split(":")[0]) return new Date().toLocaleTimeString().split(":")[0]; }; exports.currentHourString = currentHourString; /** * * @returns yesterdays date, with the current time */ const yesterdaysDate = () => { const formedDate = new Date(); const time = formedDate.toLocaleTimeString().split(":"); const actualMonth = (exports.indexOfThrityDaysMonth.includes(formedDate.getMonth()) && formedDate.getDate() === 1) ? formedDate.getMonth() - 1 : (exports.indexOfThrityOneDaysMonth.includes(formedDate.getMonth()) && formedDate.getDate() === 1) ? formedDate.getMonth() - 1 : (formedDate.getDate() === 28 && formedDate.getFullYear() % 4 !== 0 && formedDate.getMonth() === 1) ? 2 // gives March : (formedDate.getDate() === 29 && formedDate.getFullYear() % 4 !== 0 && formedDate.getMonth() === 1) ? 2 // gives March : formedDate.getMonth(); const actualYesterdaysDate = (formedDate.getDate() === 1 && exports.indexOfThrityDaysMonth.includes(formedDate.getMonth())) ? exports.monthAndNumberOfDays[exports.monthAndIndex[formedDate.getMonth() - 1]] : (formedDate.getDate() === 1 && exports.indexOfThrityOneDaysMonth.includes(formedDate.getMonth())) ? exports.monthAndNumberOfDays[exports.monthAndIndex[formedDate.getMonth() - 1]] : (formedDate.getDate() === 28 && formedDate.getFullYear() % 4 !== 0 && formedDate.getMonth() === 1) ? 28 : (formedDate.getDate() === 29 && formedDate.getFullYear() % 4 !== 0 && formedDate.getMonth() === 1) ? 29 : formedDate.getDate() - 1; const ydate = new Date(currentYear, actualMonth, actualYesterdaysDate, (0, exports.actualHour)(time), +time[1], +time[2].split(" ")[0]); return ydate; }; exports.yesterdaysDate = yesterdaysDate; /** * * @returns returns tomorrows date with the current time */ const tomorrowsDate = () => { const formedDate = new Date(); const time = formedDate.toLocaleTimeString().split(":"); const actualMonth = (exports.indexOfThrityDaysMonth.includes(formedDate.getMonth()) && formedDate.getDate() === 30) ? formedDate.getMonth() + 1 : (exports.indexOfThrityOneDaysMonth.includes(formedDate.getMonth()) && formedDate.getDate() === 31) ? formedDate.getMonth() + 1 : (formedDate.getDate() === 28 && formedDate.getFullYear() % 4 !== 0 && formedDate.getMonth() === 1) ? 2 // gives March : (formedDate.getDate() === 29 && formedDate.getFullYear() % 4 === 0 && formedDate.getMonth() === 1) ? 2 // gives March : formedDate.getMonth(); const actualTomorowsDate = (exports.indexOfThrityDaysMonth.includes(formedDate.getMonth()) && formedDate.getDate() === 30) ? 1 : (exports.indexOfThrityOneDaysMonth.includes(formedDate.getMonth()) && formedDate.getDate() === 31) ? 1 : (formedDate.getDate() === 28 && formedDate.getFullYear() % 4 !== 0 && formedDate.getMonth() === 1) ? 1 : (formedDate.getDate() === 29 && formedDate.getFullYear() % 4 !== 0 && formedDate.getMonth() === 1) ? 1 : formedDate.getDate() + 1; const tomorrowsDate = new Date(currentYear, actualMonth, actualTomorowsDate, (0, exports.actualHour)(time), +time[1], +time[2].split(" ")[0]); return tomorrowsDate; }; exports.tomorrowsDate = tomorrowsDate; /** * * @param {*} date an optional parametre of date or string type which when provided, * the function converts to a date type and returns the current time * @returns */ const givenDateAndCurrentTime = (date) => { const formedDate = new Date(); const time = formedDate.toLocaleTimeString().split(":"); let requiredDate = undefined; if (typeof (date) === "string") { const splittedStringDate = date.includes("-") ? date.split("-") : date.split("/"); requiredDate = new Date(+splittedStringDate[0], +splittedStringDate[1] - 1, +splittedStringDate[2], (0, exports.actualHour)(time), +time[1], +time[2].split(" ")[0]); return requiredDate; } else if (date) { const splittedStringDate = date.toLocaleDateString().split("/"); requiredDate = new Date(+splittedStringDate[2], +splittedStringDate[0] - 1, +splittedStringDate[1], (0, exports.actualHour)(time), +time[1], +time[2].split(" ")[0]); return requiredDate; } requiredDate = new Date(formedDate.getFullYear(), formedDate.getMonth(), formedDate.getDate(), (0, exports.actualHour)(time), +time[1], +time[2].split(" ")[0]); // console.log("\n\t yesterdaysDate-no-date-given: ", yesterdaysDate()) return requiredDate; }; exports.givenDateAndCurrentTime = givenDateAndCurrentTime; /** * * @param date can be of the form "YYYY-MM-DD", "YYYY/MM/DD" or "Sun Sep 11 2022" * @returns numerical value of month for the given date or current dat, if no argument was passed */ const getMonth = (date) => { if (typeof (date) === "string") { const formedDate = (0, exports.givenDateAndCurrentTime)(date); return formedDate.getMonth(); } else if (date) { const splittedStringDate = date.toLocaleDateString().split("/"); return +splittedStringDate[0]; } const formedDate = (0, exports.todaysDate)(); return (formedDate.getMonth()) + 1; }; exports.getMonth = getMonth; const dateAcronym = (day) => { const numLen = String(day).length; const stringifiedDay = numLen === 1 ? `0${day}` : `${day}`; // console.log("\n\t stringifiedDay: ", stringifiedDay, +stringifiedDay[1]) if (day > 0 && day <= 31) { if (+stringifiedDay[1] !== 0 && stringifiedDay.includes("1") && +stringifiedDay[0] !== 1) { return 'st'; } else if (+stringifiedDay[1] !== 0 && !stringifiedDay.includes("1") && +stringifiedDay[1] <= 3) { if (stringifiedDay.includes("3")) { return 'rd'; } return "nd"; } else { return 'th'; } } return "Invalid day number"; }; exports.dateAcronym = dateAcronym; const todaysDate = () => { return (0, exports.givenDateAndCurrentTime)(); }; exports.todaysDate = todaysDate; const differenceInDays = (firstDate, secondDate) => { // console.log("\n\t differenceInDays-firstDate: ", firstDate) // console.log("\n\t differenceInDays-secondDate: ", secondDate) try { let actualFirstDate = new Date(); let actualSecondDate = new Date(); if (typeof (firstDate) === "string") { actualFirstDate = new Date(firstDate); } if (typeof (secondDate) === "string") { actualSecondDate = new Date(secondDate); } else { actualFirstDate = firstDate; actualSecondDate = secondDate; } const yearOfFirstDateToCompare = actualFirstDate.getFullYear(); const monthOfFirstDateToCompare = actualFirstDate.getMonth(); const dayOfFirstDateToCompare = actualFirstDate.getDate(); const yearOfSecondDateToCompare = actualSecondDate.getFullYear(); const monthOfSecondDateToCompare = actualSecondDate.getMonth(); const dayOfSecondDateToCompare = actualSecondDate.getDate(); if (yearOfFirstDateToCompare === yearOfSecondDateToCompare) { if (monthOfFirstDateToCompare === monthOfSecondDateToCompare) { return Math.abs(dayOfSecondDateToCompare - dayOfFirstDateToCompare); } const monthOfFirstDateInWords = (Object.values(exports.monthAndIndex))[monthOfFirstDateToCompare]; const numberOfDaysInMonthOfFirstDate = exports.monthAndNumberOfDays[monthOfFirstDateInWords]; const differenceBetweenDayOfFirstDateToCompareAndNumberOfDaysInTheMonthOfFirstDate = numberOfDaysInMonthOfFirstDate - dayOfFirstDateToCompare; const firstDateInSecondMonth = new Date(`${monthOfSecondDateToCompare}/01/${yearOfSecondDateToCompare}`); const differenceBetweenSecondDateAndFirstDayOfTheMonth = dayOfSecondDateToCompare - firstDateInSecondMonth.getDate(); const dayDifferenInMonthOfSecondDate = differenceBetweenDayOfFirstDateToCompareAndNumberOfDaysInTheMonthOfFirstDate + differenceBetweenSecondDateAndFirstDayOfTheMonth + 1; // console.log("\n\t dayDifferenInMonthOfSecondDate: ", dayDifferenInMonthOfSecondDate) return Math.abs(dayDifferenInMonthOfSecondDate); } else { return false; } } catch (error) { // console.log("\n\t differenceInDays-error: ", error) } }; exports.differenceInDays = differenceInDays; /** * This function gives the date of the next day, in actual human context. * A simple way of understanding the function is by saying; give me n days from now. * @param year * @param month javascript month structure, where january is indexed as 0 * @param dayIncrementor if this value is supplied, then the function increaes * the date by this value; default value is one as expected */ const dateForwardBuild = (year, month, dayIncrementor) => { const currentDate = new Date(); const time = currentDate.toLocaleTimeString().split(":"); const formedDate = new Date(year, month, currentDate.getDate(), (0, exports.actualHour)(time), +time[1], +time[2].split(" ")[0]); const actualMonth = (exports.indexOfThrityDaysMonth.includes(formedDate.getMonth()) && formedDate.getDate() === 1) ? formedDate.getMonth() - 1 : (exports.indexOfThrityOneDaysMonth.includes(formedDate.getMonth()) && formedDate.getDate() === 1) ? formedDate.getMonth() - 1 : (formedDate.getDate() === 28 && formedDate.getFullYear() % 4 !== 0 && formedDate.getMonth() === 1) ? 2 // gives March : (formedDate.getDate() === 29 && formedDate.getFullYear() % 4 !== 0 && formedDate.getMonth() === 1) ? 2 // gives March : formedDate.getMonth(); const numberOfDaysInActualMonth = exports.monthAndNumberOfDays[exports.monthAndIndex[actualMonth]]; if (dayIncrementor) { if (currentDate.getDate() + dayIncrementor < numberOfDaysInActualMonth) { const nextDate = new Date(year, month, currentDate.getDate() + dayIncrementor, (0, exports.actualHour)(time), +time[1], +time[2].split(" ")[0]); return nextDate; } const actualIncrement = currentDate.getDate() + dayIncrementor - numberOfDaysInActualMonth; const nextDate = new Date(year, month + 1, actualIncrement === 0 ? 1 : actualIncrement, (0, exports.actualHour)(time), +time[1], +time[2].split(" ")[0]); return nextDate; } ; let nextDate = undefined; if (currentDate.getDate() === numberOfDaysInActualMonth) { nextDate = new Date(year, month + 1, 1, (0, exports.actualHour)(time), +time[1], +time[2].split(" ")[0]); return nextDate; } ; nextDate = new Date(year, month, currentDate.getDate() + 1, (0, exports.actualHour)(time), +time[1], +time[2].split(" ")[0]); return nextDate; }; exports.dateForwardBuild = dateForwardBuild; const dateConstrainer = (date) => { let dateData = undefined; if (typeof (date) === "string") { dateData = (0, exports.givenDateAndCurrentTime)(date); } else { dateData = new Date(date.getFullYear(), date.getMonth(), date.getDate()); } const madeDate = `${dateData.getFullYear()}-${dateData.getMonth() + 1 >= 10 ? dateData.getMonth() + 1 : "0" + (dateData.getMonth() + 1)}-${dateData.getDate() >= 10 ? dateData.getDate() : "0" + (dateData.getDate())}`; return madeDate; }; exports.dateConstrainer = dateConstrainer; /** * Specifically for html forms; to ensure the choosable date begins from yesterday and backward * @returns a string constrain to allow choosable dates from yesterday alone * Simple usage = (document as any).querySelector(".arrival-date").max = yesterdaysFormDate(); */ const yesterdaysFormDate = () => { const date = new Date(); const today = `${date.getFullYear()}-${date.getMonth() + 1 >= 10 ? date.getMonth() + 1 : "0" + (date.getMonth() + 1)}-${date.getDate() >= 10 ? date.getDate() - 1 : "0" + (date.getDate() - 1)}`; return today; }; exports.yesterdaysFormDate = yesterdaysFormDate; /** * Specifically for html forms; to ensure the choosable date begins from tomorrow and forward * @returns a string constrain to allow choosable dates from yesterday alone * Simple usage = (document as any).querySelector(".arrival-date").max = yesterdaysFormDate(); */ const tomorrowsFormDate = () => { const date = new Date(); const today = `${date.getFullYear()}-${date.getMonth() + 1 >= 10 ? date.getMonth() + 1 : "0" + (date.getMonth() + 1)}-${date.getDate() >= 10 ? date.getDate() + 1 : "0" + (date.getDate() + 1)}`; return today; }; exports.tomorrowsFormDate = tomorrowsFormDate; /** * Specifically for html forms; to ensure the choosable date begins from tomorrow and forward * @returns a string constrain to allow choosable dates from tomorrow alone * Simple usage = (document as any).querySelector(".arrival-date").max = todaysFormDate(); */ const todaysFormDate = () => { const date = new Date(); const today = `${date.getFullYear()}-${date.getMonth() + 1 >= 10 ? date.getMonth() + 1 : "0" + (date.getMonth() + 1)}-${date.getDate() >= 10 ? date.getDate() : "0" + date.getDate()}`; return today; }; exports.todaysFormDate = todaysFormDate;