UNPKG

date-fran

Version:

Date functionalities for javascript projects.

182 lines (181 loc) 8.32 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.dateStringToDate = exports.tomorrowsDateByGivenDate = exports.yesterdaysDateByGivenDate = exports.sameDateComparator = exports.varyingDatesComparator = void 0; const builders_1 = require("./builders"); /** * Think of the function as saying; future date should be greater than past date. * If both dates are same, the function returns false; implying that the future date * exceedes the other. * @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) => { let actualpastDate = pastDate; let actualfutureDate = futureDate; if (typeof (pastDate) === "string") { actualpastDate = (0, builders_1.givenDateAndCurrentTime)(pastDate); } if (typeof (futureDate) === "string") { actualfutureDate = (0, builders_1.givenDateAndCurrentTime)(futureDate); } ; const yearOfpastDate = actualpastDate.getFullYear(); const yearOffutureDate = actualfutureDate.getFullYear(); const monthOfpastDate = actualpastDate.getMonth(); const monthOffutureDate = actualfutureDate.getMonth(); const dayOfpastDate = actualpastDate.getDate(); const dayOffutureDate = actualfutureDate.getDate(); if (yearOffutureDate < yearOfpastDate) { return false; } if (yearOffutureDate > yearOfpastDate) { return true; } if (monthOffutureDate > monthOfpastDate) { return true; } if (monthOffutureDate < monthOfpastDate) { return false; } if (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, builders_1.givenDateAndCurrentTime)(firstDate); } if (typeof (secondDate) === "string") { actualfutureDate = (0, builders_1.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 (dateData.includes("T")) { const splittedDate = dateData.split("T")[0]; reqDate = (0, builders_1.givenDateAndCurrentTime)(splittedDate); } else { reqDate = (0, builders_1.givenDateAndCurrentTime)(dateData); } const year = reqDate.getFullYear(); const month = reqDate.getMonth(); const day = reqDate.getDate(); const formedDate = new Date(); const time = formedDate.toLocaleTimeString().split(":"); const actualMonth = (builders_1.indexOfThrityDaysMonth.includes(month) && day === 1) ? month - 1 : (builders_1.indexOfThrityOneDaysMonth.includes(month) && day === 1) ? month - 1 : (day === 28 && year % 4 !== 0 && month === 1) ? 2 // gives March : (day === 29 && year % 4 !== 0 && month === 1) ? 2 // gives March : month; const actualYesterdaysDate = (day === 1 && builders_1.indexOfThrityDaysMonth.includes(month)) ? builders_1.monthAndNumberOfDays[(builders_1.monthAndIndex)[month - 1]] : (day === 1 && builders_1.indexOfThrityOneDaysMonth.includes(month)) ? builders_1.monthAndNumberOfDays[(builders_1.monthAndIndex)[month - 1]] : (day === 28 && year % 4 !== 0 && month === 1) ? 28 : (day === 29 && year % 4 !== 0 && month === 1) ? 29 : day - 1; const ydate = new Date(year, actualMonth, actualYesterdaysDate, (0, builders_1.actualHour)(time), +time[1], +time[2].split(" ")[0]); // console.log("\n\t yesterdaysDateByGivenDate: ", ydate.toDateString()) // console.log("\n\t tomorrowsDate: ", tomorrowsDate()) 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; if (typeof (dateData) === "string") { if (dateData.includes("T")) { const splittedDate = dateData.split("T")[0]; reqDate = (0, builders_1.givenDateAndCurrentTime)(splittedDate); } else { reqDate = (0, builders_1.givenDateAndCurrentTime)(dateData); } } else { reqDate = dateData; } const year = reqDate.getFullYear(); const month = reqDate.getMonth(); const day = reqDate.getDate(); const formedDate = new Date(); const time = formedDate.toLocaleTimeString().split(":"); const actualMonth = (builders_1.indexOfThrityDaysMonth.includes(month) && day === 30) ? month + 1 : (builders_1.indexOfThrityOneDaysMonth.includes(month) && day === 31) ? month + 1 : (day === 28 && year % 4 !== 0 && month === 1) ? 2 // gives March : (day === 29 && year % 4 === 0 && month === 1) ? 2 // gives March : month; const actualTomorowsDate = (builders_1.indexOfThrityDaysMonth.includes(month) && day === 30) ? 1 : (builders_1.indexOfThrityOneDaysMonth.includes(month) && day === 31) ? 1 : (day === 28 && year % 4 !== 0 && month === 1) ? 1 : (day === 29 && year % 4 !== 0 && month === 1) ? 1 : day + 1; const tomorrowsDate = new Date(year, actualMonth, actualTomorowsDate, (0, builders_1.actualHour)(time), +time[1], +time[2].split(" ")[0]); // console.log("\n\t todaysDate: ", todaysDate()); 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(builders_1.monthAndIndex).indexOf(desciptionDateToReverse[1]); const descriptionDate = desciptionDateToReverse[2]; const descriptionYear = desciptionDateToReverse[3]; const date = (0, builders_1.givenDateAndCurrentTime)(`${descriptionYear}-${descriptionMonth + 1}-${descriptionDate}`); return date; } return "Invalid date argument"; }; exports.dateStringToDate = dateStringToDate;