UNPKG

date-fran

Version:

Date functionalities for javascript projects.

781 lines (780 loc) 35.1 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.datesInTheMonth = exports.dateCountBuildFromStartDateToMaximum = exports.listDatesBetweenBoundaries = exports.dateCountBuilderByBoundary = exports.giveMeDatesWithinBoundary = exports.givenFormDate = exports.yearCount = exports.dateCountBuilder = exports.currentTime = exports.dateStringToDate = exports.tomorrowsDateByGivenDate = exports.yesterdaysDateByGivenDate = exports.sameDateComparator = exports.varyingDatesComparator = 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.monthNumber = exports.currentHourString = exports.actualHour = exports.monthAndIndex = exports.monthAndNumberOfDays = exports.indexOfThrityOneDaysMonth = exports.indexOfThrityDaysMonth = void 0; const currentYear = new Date().getFullYear(); 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) => { // console.log("\n\t actualHour-Time: ", 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 = () => { return new Date().toLocaleTimeString().split(":")[0]; }; exports.currentHourString = currentHourString; /** * * @param date * @returns the actual (non-JS) numerical month value of a given date */ const monthNumber = (date) => { const month = date.toDateString().split(" ")[1]; const monthNumber = Object.values(exports.monthAndIndex).indexOf(month); return monthNumber; }; exports.monthNumber = monthNumber; /** * * @returns yesterdays date, with the current time */ const yesterdaysDate = () => { const currentYear = new Date().getFullYear(); const formedDate = new Date(); const time = formedDate.toLocaleTimeString().split(":"); // console.log("\n\t formedDate-getMonth: ", formedDate.getMonth()) // const formedDate.getHours() = (formedDate.getHours() === 0 || formedDate.getHours() === 23) ? 22 : formedDate.getHours(); const actualMonth = (exports.indexOfThrityDaysMonth.includes(formedDate.getMonth()) && formedDate.getDate() === 1) ? formedDate.getMonth() - 1 : (exports.indexOfThrityOneDaysMonth.includes(formedDate.getMonth()) && formedDate.getDate() === 1) ? 11 : (formedDate.getMonth() === 2 && formedDate.getDate() === 1) ? 1 // gives March : formedDate.getMonth(); // console.log("\n\t actualMonth: ", Math.abs(actualMonth),) // console.log("\n\t actualMonth: ", Math.abs(actualMonth),) 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())) ? 31 : (formedDate.getFullYear() % 4 !== 0 && formedDate.getMonth() === 2 && formedDate.getDate() === 1) ? 28 : (formedDate.getFullYear() % 4 === 0 && formedDate.getMonth() === 2 && formedDate.getDate() === 1) ? 29 : formedDate.getDate() - 1; // console.log("\n\t formedDate.getDate(): ", formedDate.getDate()); // console.log("\n\tactualYesterdaysDate: ",actualYesterdaysDate); // console.log("\n\t indexOfThrityOneDaysMonth.includes(formedDate.getMonth(): ", indexOfThrityOneDaysMonth.includes(formedDate.getMonth()); const actualYear = formedDate.getDate() === 1 && exports.indexOfThrityOneDaysMonth.includes(formedDate.getMonth()) ? currentYear - 1 : currentYear; // console.log("\n\t actualYear: ", actualYear); const ydate = new Date(actualYear, Math.abs(actualMonth), actualYesterdaysDate, formedDate.getHours(), +time[1], +time[2].split(" ")[0]); // console.log("\n\t tomorrowsDate: ", tomorrowsDate()) return ydate; }; exports.yesterdaysDate = yesterdaysDate; /** * * @returns returns tomorrows date with the current time */ const tomorrowsDate = () => { const formedDate = new Date(); const actualDate = formedDate.getHours() === 0 ? formedDate.getDate() + 1 : formedDate.getDate(); const actualHours = formedDate.getHours() === 0 ? 1 : formedDate.getHours(); 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 : actualDate + 1; const tomorrowsDate = new Date(currentYear, actualMonth, actualTomorowsDate, actualHours, formedDate.getMinutes(), formedDate.getSeconds()); return tomorrowsDate; }; exports.tomorrowsDate = tomorrowsDate; /** * * @param {*} date an optional parametre of date or string (in the form YYYY-mm-dd or YYYY/mm/dd) type which when provided, * the function returns a value of that date and current time as JS-date data type * @returns */ const givenDateAndCurrentTime = (date) => { const formedDate = new Date(); let requiredDate; const actualHours = (formedDate.getHours() === 0 || formedDate.getHours() === 1) ? 2 : formedDate.getHours(); // console.log("\n\t actualHours: ", actualHours); if (typeof (date) === "string") { const splittedStringDate = date.includes("-") ? date.split("-") : date.split("/"); const actualDate = formedDate.getHours() + 1 === 1 ? +splittedStringDate[2] : splittedStringDate[2]; // const actualDate = formedDate.getHours() === 23 ? +splittedStringDate[2]+1 // console.log("\n\t splittedStringDate[1]: ",splittedStringDate[1]) requiredDate = new Date(+splittedStringDate[0], +splittedStringDate[1] - 1, +actualDate, actualHours, formedDate.getMinutes(), formedDate.getSeconds()); return requiredDate; } else if (date) { const actualDate = formedDate.getHours() + 1 === 1 ? date.getDate() : date.getDate(); // console.log("\n\t Date-type-date", date) requiredDate = new Date(date.getFullYear(), date.getMonth(), actualDate, actualHours, formedDate.getMinutes(), formedDate.getSeconds()); return requiredDate; } ; const actualDate = formedDate.getHours() + 1 === 1 ? formedDate.getDate() : formedDate.getDate(); requiredDate = new Date(formedDate.getFullYear(), formedDate.getMonth(), actualDate, actualHours, formedDate.getMinutes(), formedDate.getSeconds()); return requiredDate; }; exports.givenDateAndCurrentTime = givenDateAndCurrentTime; /** * * @param date can be of the form "YYYY-MM-DD", "YYYY/MM/DD" or "Sun Sep 11 2022" * @returns a non-JS numerical value of month for the given date or current date, if no argument was passed */ const getMonth = (date) => { if (typeof (date) === "string") { const formedDate = (0, exports.givenDateAndCurrentTime)(date); return formedDate.getMonth() + 1; } else if (date) { const splittedStringDate = date.toLocaleDateString().split("/"); return +splittedStringDate[0]; } return ((0, exports.todaysDate)().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 = (0, exports.givenDateAndCurrentTime)(); let actualSecondDate = (0, exports.givenDateAndCurrentTime)(); if (typeof (firstDate) === "string") { actualFirstDate = (0, exports.givenDateAndCurrentTime)(firstDate); } else { actualFirstDate = firstDate; } if (typeof (secondDate) === "string") { actualSecondDate = (0, exports.givenDateAndCurrentTime)(secondDate); } else { actualSecondDate = secondDate; } const dateDiff = (Math.abs(actualFirstDate - actualSecondDate)) / (3600 * 1000 * 24); return String(dateDiff).split(".")[0]; } 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.getHours() = currentDate.getHours() === 23 ? 22 : currentDate.getHours()+1; const formedDate = new Date(year, month, currentDate.getDate(), currentDate.getHours(), +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, formedDate.getHours(), +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, formedDate.getHours(), +time[1], +time[2].split(" ")[0]); return nextDate; } ; let nextDate = undefined; if (currentDate.getDate() === numberOfDaysInActualMonth) { nextDate = new Date(year, month + 1, 1, formedDate.getHours(), +time[1], +time[2].split(" ")[0]); return nextDate; } ; nextDate = new Date(year, month, currentDate.getDate() + 1, formedDate.getHours(), +time[1], +time[2].split(" ")[0]); return nextDate; }; exports.dateForwardBuild = dateForwardBuild; const dateConstrainer = (date) => { let dateData; 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; // import { actualHour, givenDateAndCurrentTime, indexOfThrityDaysMonth, indexOfThrityOneDaysMonth, monthAndIndex, monthAndNumberOfDays } from "./builders"; /** * Think of the function as saying; future date should be greater than past date.. * @param pastDate: string or Date data type. string must follow the format YYYY-MM-DD or YYYY/MM/DD * @param futureDate: string or Date data type. string must follow the format YYYY-MM-DD or YYYY/MM/DD * @returns true if future date is greater than past date */ const varyingDatesComparator = (pastDate, futureDate) => { // console.log("\n\t varyingDatesComparator-pastDate: ", pastDate); // console.log("\n\t varyingDatesComparator-futureDate: ", futureDate); let actualpastDate = pastDate; let actualfutureDate = futureDate; if (typeof (pastDate) === "string") { actualpastDate = (0, exports.givenDateAndCurrentTime)(pastDate); } if (typeof (futureDate) === "string") { actualfutureDate = (0, exports.givenDateAndCurrentTime)(futureDate); } ; const yearOfpastDate = actualpastDate.getFullYear(); const yearOffutureDate = actualfutureDate.getFullYear(); const monthOfpastDate = actualpastDate.getMonth(); const monthOffutureDate = actualfutureDate.getMonth(); // console.log("\n\t monthOffutureDate, monthOfpastDate: ", monthOffutureDate, monthOfpastDate) const dayOfpastDate = actualpastDate.getDate(); const dayOffutureDate = actualfutureDate.getDate(); if (yearOffutureDate < yearOfpastDate) { // console.log("\n\t yearOffutureDate < yearOfpastDate") return false; } if (monthOffutureDate < monthOfpastDate) { // console.log("\n\t monthOffutureDate < monthOfpastDate") return false; } if (monthOffutureDate === monthOfpastDate && dayOffutureDate < dayOfpastDate) { // console.log("\n\t dayOffutureDate < dayOfpastDate: ", dayOffutureDate, dayOfpastDate) return false; } return true; }; exports.varyingDatesComparator = varyingDatesComparator; // console.log("\n\t dateComparator: ", varyingDatesComparator(yesterdaysDate(), givenDateAndCurrentTime("2022-08-30"))); /** * Imagine the function saying; both dates are equal * @param firstDate can be of the form "YYYY-MM-DD", "YYYY/MM/DD" or "Sun Sep 11 2022" * @param secondDate can be of the form "YYYY-MM-DD", "YYYY/MM/DD" or "Wed Oct 05 2021" * @returns true if both dates are equal. Otherwise, false */ const sameDateComparator = (firstDate, secondDate) => { if (firstDate === secondDate) { return true; } else { let actualFirstDate = firstDate; let actualfutureDate = secondDate; if (typeof (firstDate) === "string") { actualFirstDate = (0, exports.givenDateAndCurrentTime)(firstDate); } if (typeof (secondDate) === "string") { actualfutureDate = (0, exports.givenDateAndCurrentTime)(secondDate); } ; const yearOfFirstDate = actualFirstDate.getFullYear(); const yearOffutureDate = actualfutureDate.getFullYear(); const monthOfFirstDate = actualFirstDate.getMonth(); const monthOffutureDate = actualfutureDate.getMonth(); const dayOfFirstDate = actualFirstDate.getDate(); const dayOffutureDate = actualfutureDate.getDate(); if (yearOffutureDate === yearOfFirstDate) { if (monthOffutureDate === monthOfFirstDate) { if (dayOffutureDate === dayOfFirstDate) { // console.log("|n\t dayOffutureDate ",dayOffutureDate) return true; } } } ; return false; } }; exports.sameDateComparator = sameDateComparator; /** * This function says; I'll give you yesterdays' date corresponidng to the given * date you give me. * @param dateData a date-data of the form "YYYY-MM-DD", "YYYY/MM/DD" or "YYY-MM-DDTHH:mm:ss.000Z" * @returns */ const yesterdaysDateByGivenDate = (dateData) => { let reqDate = dateData; if (typeof (dateData) === "string" && dateData.includes("T")) { const splittedDate = String(dateData).split("T")[0]; reqDate = (0, exports.givenDateAndCurrentTime)(splittedDate); } else { reqDate = (0, exports.givenDateAndCurrentTime)(dateData); } ; // console.log("\n\t dateData: ", dateData) const year = reqDate.getFullYear(); const month = reqDate.getMonth(); const day = reqDate.getDate(); const formedDate = new Date(); const isLeapYear = year % 4 === 0 ? true : false; const givenMonthValue = month - 1 < 0 ? 0 : month - 1; const actualMonth = day === 1 ? month - 1 : month; // const realMonth = !actualMonth ? 1 : actualMonth; // console.log("\n\t actualMonth: ", actualMonth) const actualYesterdaysDate = (day === 1 && month !== 2) ? exports.monthAndNumberOfDays[exports.monthAndIndex[givenMonthValue]] : (month === 2 && day === 1) ? year % 4 === 0 ? 29 : 28 : reqDate.getHours() === 23 && reqDate.getDate() % 2 !== 0 ? day - 2 : day - 1; // const realYesterdaysDate = !actualYesterdaysDate ? 31 : actualYesterdaysDate; // console.log("\n\t year: ", year) // const realYear = actualMonth < 0 && day === 1 ? year-1 : year; // console.log("\n\t realYear: ", realYear) const ydate = new Date(year, actualMonth, actualYesterdaysDate, formedDate.getHours(), formedDate.getMinutes()); return ydate; }; exports.yesterdaysDateByGivenDate = yesterdaysDateByGivenDate; /** * This function says; I'll give you tomorrows' date corresponidng to the given * date you give me. * @param dateData a date-data of the form "YYYY-MM-DD", "YYYY/MM/DD" or "YYY-MM-DDTHH:mm:ss.000Z" * @returns */ const tomorrowsDateByGivenDate = (dateData) => { let reqDate = dateData; // console.log("\n\t tomorrowsDateByGivenDate-func-givenDate-dateData: ",dateData);; if (typeof (dateData) === "string") { if (dateData.includes("T")) { const splittedDate = dateData.split("T")[0]; reqDate = (0, exports.givenDateAndCurrentTime)(splittedDate); } else { reqDate = (0, exports.givenDateAndCurrentTime)(dateData); } } else { reqDate = dateData; } ; // console.log("\n\t Test--reqDate: ", reqDate); const year = reqDate.getFullYear(); const month = reqDate.getMonth(); const day = reqDate.getDate(); const formedDate = new Date(); const isLeapYear = year % 4 === 0 ? true : false; // const time = reqDate.toLocaleTimeString().split(":"); // const formedDate.getHours() = (formedDate.getHours() === 0 || formedDate.getHours() === 23) ? 22 : formedDate.getHours(); const actualMonth = (exports.indexOfThrityDaysMonth.includes(month) && day === 30) ? month + 1 : (exports.indexOfThrityOneDaysMonth.includes(month) && day === 31) ? month + 1 // : (year % 4 !== 0 && month === 2) ? 2 // gives March : (month === 1 && (day === 28 && !isLeapYear)) ? 2 // gives March : (month === 1 && (day === 29 && isLeapYear)) ? 2 // gives March : month; const actualTomorowsDate = (exports.indexOfThrityDaysMonth.includes(month) && day === 30) ? 1 : (exports.indexOfThrityOneDaysMonth.includes(month) && day === 31) ? 1 : (month === 1 && (day === 28 && !isLeapYear)) ? 1 : (month === 1 && (day === 29 && isLeapYear)) ? 1 : formedDate.getHours() === 0 ? day : formedDate.getHours() === 23 ? day + 2 : day + 1; const tomorrowsDate = new Date(year, actualMonth, actualTomorowsDate, formedDate.getHours(), formedDate.getMinutes()); return tomorrowsDate; }; exports.tomorrowsDateByGivenDate = tomorrowsDateByGivenDate; /** * convert dates of the format: Mon Oct 17 2022; to actual date types * @param dateString * @returns */ const dateStringToDate = (dateString) => { const desciptionDateToReverse = dateString.split(" "); // if (desciptionDateToReverse.length > 0) { // console.log("\n\t desciptionDateToReverse: ", desciptionDateToReverse) const descriptionMonth = Object.values(exports.monthAndIndex).indexOf(desciptionDateToReverse[1]); const descriptionDate = desciptionDateToReverse[2]; const descriptionYear = desciptionDateToReverse[3]; const date = (0, exports.givenDateAndCurrentTime)(`${descriptionYear}-${descriptionMonth + 1}-${descriptionDate}`); return date; } return "Invalid date argument"; }; exports.dateStringToDate = dateStringToDate; /** * * @returns The current local time as AM or PM */ const currentTime = () => { const time = (0, exports.todaysDate)().toLocaleTimeString(); const hourTime = time.split(":")[0]; // console.log("\n\t current time: ", time) let requiredTime = ""; if (+hourTime - 1 < 12) { requiredTime = `${+hourTime - 1 === 0 ? 12 : +hourTime - 1}:${time.split(":")[1]}:${time.split(":")[2].split(" ")[0]}`.concat(" ", "AM"); } else { requiredTime = `${+hourTime - 1 === 0 ? 12 : +hourTime - 1}:${time.split(":")[1]}:${time.split(":")[2].split(" ")[0]}`.concat(" ", "PM"); } ; // console.log("\n\t current requiredTime: ", requiredTime) return requiredTime; }; exports.currentTime = currentTime; /** * * @param maxCount The maximum count to return i.e to return 70 days forward or backward of * todays date, enter 70. * @param direction if the function should return future dates or past dates * @param type the entity format to be returned in the array either: * "Sat Oct 29 2022" as string or 2022-10-29T14:04:31.000+00:00 as Date * @returns array of strings or date */ const dateCountBuilder = (maxCount, direction, type) => { let todaysCurrentDate = (0, exports.todaysDate)(); let count = 1; const dateStrings = []; dateStrings.push(todaysCurrentDate.toDateString()); if (direction === "down") { while (count < maxCount) { let yestDate = (0, exports.yesterdaysDateByGivenDate)(todaysCurrentDate); dateStrings.push(type === "string" ? yestDate.toDateString() : yestDate); todaysCurrentDate = yestDate; count++; } } else { while (count < maxCount) { let tomorrowsDate = (0, exports.tomorrowsDateByGivenDate)(todaysCurrentDate); dateStrings.push(type === "string" ? tomorrowsDate.toDateString() : tomorrowsDate); todaysCurrentDate = tomorrowsDate; count++; } } ; return dateStrings; }; exports.dateCountBuilder = dateCountBuilder; /** * * @param maxCount The maximum count to return i.e to return 70 days forward or backward of * todays date, enter 70. * @param direction if the function should return future years or past years * @returns array of year number */ const yearCount = (maxCount, direction) => { let currentYear = (0, exports.todaysDate)().getFullYear(); let count = 1; const dateStrings = []; dateStrings.push(currentYear); if (direction === "down") { while (count < maxCount) { let yestYear = currentYear - 1; dateStrings.push(yestYear); currentYear = yestYear; count++; } } else { while (count < maxCount) { let tomorrowsYear = currentYear + 1; dateStrings.push(tomorrowsYear); currentYear = tomorrowsYear; count++; } } ; return dateStrings; }; exports.yearCount = yearCount; /** * restrict date selection start or stop on forms * @param dateVal * @returns */ const givenFormDate = (dateVal) => { const today = `${dateVal.getFullYear()}-${dateVal.getMonth() + 1 >= 10 ? dateVal.getMonth() + 1 : "0" + (dateVal.getMonth() + 1)}-${dateVal.getDate() >= 10 ? dateVal.getDate() : "0" + dateVal.getDate()}`; return today; }; exports.givenFormDate = givenFormDate; /** * * @param startDate the date to start the boundary * @param endDate the date to end the boundary * @returns an array of string date(s) within boundary or a string, if an error occurred */ const giveMeDatesWithinBoundary = (startDate, endDate) => { // console.log("\n\t giveMeDatesWithinBoundary: ", startDate, endDate) const stringStartDate = startDate.toDateString(); const stringEndDate = endDate.toDateString(); let errorMessage = ""; errorMessage = "Are you sure you gave a date?"; if (typeof (startDate) !== "string" && typeof (endDate) !== "string") { const dateCompare = (0, exports.varyingDatesComparator)(startDate, endDate); errorMessage = "Are you sure endDate is above startDate?"; if (dateCompare) { let currentDate = ""; const stringDates = []; if (startDate && endDate) { currentDate = stringStartDate; while (currentDate !== stringEndDate) { stringDates.push(currentDate); const dateTypeOfCurrentDate = (0, exports.dateStringToDate)(currentDate); const tomorowsDateForCurrentDate = (0, exports.tomorrowsDateByGivenDate)(dateTypeOfCurrentDate); currentDate = tomorowsDateForCurrentDate.toDateString(); } ; return stringDates; } } ; } return errorMessage; }; exports.giveMeDatesWithinBoundary = giveMeDatesWithinBoundary; /** * @param startDate either of the form "Thu Aug 17 2023" or a JavaScript date * @param maxCount The maximum count to return i.e to return 70 days forward or backward of * todays date, enter 70. * @param direction if the function should return future dates or past dates * @param type the entity format to be returned in the array either: * "Sat Oct 29 2022" as string or 2022-10-29T14:04:31.000+00:00 as Date * @returns array of strings or date */ const dateCountBuilderByBoundary = (startDate, maxCount, direction, type) => { let todaysCurrentDate = typeof (startDate) === "string" ? (0, exports.dateStringToDate)(startDate) : (0, exports.givenDateAndCurrentTime)(startDate); let count = 1; const dateStrings = []; dateStrings.push(todaysCurrentDate.toDateString()); if (direction === "down") { while (count < maxCount) { let yestDate = (0, exports.yesterdaysDateByGivenDate)(todaysCurrentDate); dateStrings.push(type === "string" ? yestDate.toDateString() : yestDate); todaysCurrentDate = yestDate; count++; } } else { while (count < maxCount) { let tomorrowsDate = (0, exports.tomorrowsDateByGivenDate)(todaysCurrentDate); dateStrings.push(type === "string" ? tomorrowsDate.toDateString() : tomorrowsDate); todaysCurrentDate = tomorrowsDate; count++; } } ; return dateStrings; }; exports.dateCountBuilderByBoundary = dateCountBuilderByBoundary; /** * * @param startDay * @param lastDayDate * @param direction * @returns an array of dates between the start and last dates */ const listDatesBetweenBoundaries = (startDay, lastDayDate, direction) => { const dayArray = []; let index = 0; let runningDay = startDay; let stop = false; while (!stop) { const convertedStringDate = (0, exports.dateStringToDate)(runningDay); const dateType = new Date(); if (typeof (convertedStringDate) === typeof (dateType)) { if ((!direction || direction === "backward") && typeof (convertedStringDate) !== "string") { const yest = (0, exports.yesterdaysDateByGivenDate)(convertedStringDate); runningDay = yest.toDateString(); dayArray.push(runningDay); } else { dayArray.push(runningDay); const tomsDate = (0, exports.tomorrowsDateByGivenDate)(convertedStringDate); runningDay = tomsDate.toDateString(); } } index++; if (runningDay === lastDayDate) { stop = true; break; } ; } return dayArray; }; exports.listDatesBetweenBoundaries = listDatesBetweenBoundaries; /** * * @param startDate date to begin computation * @param maxCount The maximum count to return i.e to return 70 days forward or backward of * todays date, enter 70. * @param direction if the function should return future dates or past dates * @param type the entity format to be returned in the array either: * "Sat Oct 29 2022" as string or 2022-10-29T14:04:31.000+00:00 as Date * @returns array of strings or date */ const dateCountBuildFromStartDateToMaximum = (startDate, maxCount, direction, type) => { let requiredStartDate = startDate; let allowContinue = true; if (typeof (startDate) === "string") { requiredStartDate = (0, exports.dateStringToDate)(startDate); if (typeof (requiredStartDate) === "string") { allowContinue = false; } } ; if (allowContinue) { let count = 1; const dateStrings = []; dateStrings.push(requiredStartDate.toDateString()); if (direction === "down") { while (count < maxCount) { let yestDate = (0, exports.yesterdaysDateByGivenDate)(requiredStartDate); dateStrings.push(type === "string" ? yestDate.toDateString() : yestDate); requiredStartDate = yestDate; count++; } } else { while (count < maxCount) { let tomorrowsDate = (0, exports.tomorrowsDateByGivenDate)(requiredStartDate); dateStrings.push(type === "string" ? tomorrowsDate.toDateString() : tomorrowsDate); requiredStartDate = tomorrowsDate; count++; } } ; return dateStrings; } else { return ["Incorrect date format"]; } }; exports.dateCountBuildFromStartDateToMaximum = dateCountBuildFromStartDateToMaximum; /** * receives a month number or month name, with respective to a particular year * and returns all possible dates in that month * @param monthNameOrNumber month name as in "Jan" or month number begining from zero * @param year the year * @param type return type i.e dates of string type or date type * @returns boolean of false or an array of the dates in the specified month */ const datesInTheMonth = (monthNameOrNumber, year, type) => { let numberOfDays = 0; let monthNumber = 0; if (typeof (monthNameOrNumber) === "string") { numberOfDays = exports.monthAndNumberOfDays[monthNameOrNumber]; Object.keys(exports.monthAndNumberOfDays).forEach((key, index) => { if (key === monthNameOrNumber) { monthNumber = index; } }); } else { const monthName = exports.monthAndIndex[monthNameOrNumber]; numberOfDays = exports.monthAndNumberOfDays[monthName]; monthNumber = monthNameOrNumber; } ; const startDate = new Date(year, monthNumber, 1); if (numberOfDays) { const datteList = (0, exports.dateCountBuilderByBoundary)(startDate, numberOfDays, "up", type); return datteList; } ; return false; }; exports.datesInTheMonth = datesInTheMonth;