Skip to content

Commit

Permalink
add getDeltaTimeFromTwoDates and minor fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniele Carlini committed Mar 1, 2024
1 parent 186dc3d commit 38d9efc
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 8 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
ts-utily / [Exports](modules.md)

**[ts-utily](README.md) / Exports

# ts-utily
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
10 changes: 10 additions & 0 deletions src/model/delta-date-result.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export interface DeltaDateResult {

years: number;
months: number,
days: number;
hours: number;
minutes: number;
seconds: number;

}
38 changes: 38 additions & 0 deletions src/utils/date-utils.ts
Original file line number Diff line number Diff line change
@@ -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).
Expand Down Expand Up @@ -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;
}
3 changes: 0 additions & 3 deletions src/utils/string-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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] : '';
});
}
45 changes: 41 additions & 4 deletions test/date-utils-test.spec.ts
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand Down Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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);
});
});

0 comments on commit 38d9efc

Please sign in to comment.