diff --git a/README.md b/README.md index 1bd79b1..e38a347 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,5 @@ +ts-utily / [Exports](modules.md) + **[ts-utily](README.md) / Exports # ts-utily diff --git a/package.json b/package.json index 742ac00..7db9c4e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ts-utily", - "version": "1.1.30", + "version": "1.2.0", "description": "Typescript utils library", "homepage": "https://github.com/kemotx90/ts-utily#readme", "main": "dist/ts-utily.js", diff --git a/src/model/delta-date-result.ts b/src/model/delta-date-result.ts new file mode 100644 index 0000000..453412c --- /dev/null +++ b/src/model/delta-date-result.ts @@ -0,0 +1,10 @@ +export interface DeltaDateResult { + + years: number; + months: number, + days: number; + hours: number; + minutes: number; + seconds: number; + +} \ No newline at end of file diff --git a/src/utils/date-utils.ts b/src/utils/date-utils.ts index 1c6e330..2dbd674 100644 --- a/src/utils/date-utils.ts +++ b/src/utils/date-utils.ts @@ -1,5 +1,6 @@ import {notPresent} from "./common-utils"; import {DayOfWeek} from "../model/day-of-week-enum"; +import {DeltaDateResult} from "../model/delta-date-result"; /** * Checks if two dates are the same (year, month, and day). @@ -248,4 +249,41 @@ export const setTime = (date: Date | number | null | undefined, time: string): D } catch (e) { console.error('not valid date or time', e); } +} + +/** + * Calculates the time difference between two dates and returns the result as an object containing the number of years, months, days, hours, minutes, and seconds. + * + * @param {Date|number} date1 - The first date. + * @param {Date|number} date2 - The second date. + * @returns {DeltaDateResult} - The result object containing the time difference in years, months, days, hours, minutes, and seconds. + */ +export const getDeltaTimeFromTwoDates = (date1: Date | number, date2: Date | number): DeltaDateResult => { + let diffInSeconds = Math.abs(new Date(date1).getTime() - new Date(date2).getTime()) / 1000; + + const years = Math.floor(diffInSeconds / 31536000); + diffInSeconds %= 31536000; + + const months = Math.floor(diffInSeconds / 2592000); + diffInSeconds %= 2592000; + + const days = Math.floor(diffInSeconds / 86400); + diffInSeconds %= 86400; + + const hours = Math.floor(diffInSeconds / 3600) % 24; + diffInSeconds %= 3600; + + const minutes = Math.floor(diffInSeconds / 60) % 60; + diffInSeconds %= 60; + + const seconds = Math.floor(diffInSeconds % 60); + + return { + years: years, + months: months, + days: days, + hours: hours, + minutes: minutes, + seconds: seconds + } as DeltaDateResult; } \ No newline at end of file diff --git a/src/utils/string-utils.ts b/src/utils/string-utils.ts index ca56a91..32b61ca 100644 --- a/src/utils/string-utils.ts +++ b/src/utils/string-utils.ts @@ -154,9 +154,6 @@ export const populateTemplate = (string: string | undefined, placeholders: any[] const placeholderRegex: RegExp = /\{(\w+?)}/g; return string?.replace(placeholderRegex, (_, name) => { const element: any = placeholders?.find(el => el[name]); - if (notPresent(element)) { - console.warn(`element {${name}} not found`) - } return element ? element[name] : ''; }); } \ No newline at end of file diff --git a/test/date-utils-test.spec.ts b/test/date-utils-test.spec.ts index 2d0964d..f6555ee 100644 --- a/test/date-utils-test.spec.ts +++ b/test/date-utils-test.spec.ts @@ -1,12 +1,13 @@ import { addDays, - dateWith00Mins, dayBetween, + dateWith00Mins, dayBetween, getDeltaTimeFromTwoDates, getHoursAndMinutesFromDateAsString, getNextDayOfWeekFromDate, getTomorrow, hoursBetween, instantToDate, isAfter, isBefore, isBetween, monthFromDate, removeDays, sameDate, setTime, toLocalDate } from "../src/index"; import {DayOfWeek} from "../src/model/day-of-week-enum"; +import {DeltaDateResult} from "../src/model/delta-date-result"; describe('sameDate function', () => { it('should return true if two dates are the same', () => { @@ -281,9 +282,12 @@ describe('getNextDayOfWeekFromDate', () => { describe('getTomorrow', () => { it('should return tomorrow\'s date when no date is provided', () => { const today = new Date(); - const expectedDate = new Date(today.getFullYear(), today.getMonth(), today.getDate() + 1); + const expectedDate = addDays(today, 1); + expectedDate.setSeconds(0 , 0); + const tomorrow = getTomorrow(); + tomorrow.setSeconds(0, 0); - expect(getTomorrow()).toStrictEqual(expectedDate); + expect(tomorrow).toStrictEqual(expectedDate); }); it('should return the next day of the provided date', () => { @@ -388,7 +392,7 @@ describe('dateWith00Mins function', () => { expect(result.getMinutes()).toBe(0); expect(result.getSeconds()).toBe(0); expect(result.getMilliseconds()).toBe(0); - expect(result.getFullYear()).toBe(2023); + expect(result.getFullYear()).toBe(new Date().getFullYear()); expect(result.getMonth()).toBe(new Date().getMonth()); expect(result.getDate()).toBe(new Date().getDate()); expect(result.getHours()).toBe(new Date().getHours()); @@ -443,4 +447,37 @@ describe('setTime function from date-utils file', () => { expect(newDate?.getHours()).toBe(12); expect(newDate?.getMinutes()).toBe(0); }); +}); + +describe('getDeltaTimeFromTwoDates', () => { + it('should return correctly formed DeltaDateResult when given valid input', () => { + const date1 = new Date(2023, 3, 19, 12, 0, 0); + const date2 = new Date(2023, 3, 20, 12, 0, 0); + + const expectedResult: DeltaDateResult = { + years: 0, + months: 0, + days: 1, + hours: 0, + minutes: 0, + seconds: 0 + } + + expect(getDeltaTimeFromTwoDates(date1, date2)).toEqual(expectedResult); + }); + + it('should return correctly formed DeltaDateResult when given two equal dates', () => { + const date1 = new Date(2023, 3, 19, 12, 0, 0); + + const expectedResult: DeltaDateResult = { + years: 0, + months: 0, + days: 0, + hours: 0, + minutes: 0, + seconds: 0 + } + + expect(getDeltaTimeFromTwoDates(date1, date1)).toEqual(expectedResult); + }); }); \ No newline at end of file