Skip to content

Commit

Permalink
feat(Days Calculator): add more diff (days, weeks...minutes)
Browse files Browse the repository at this point in the history
  • Loading branch information
sharevb committed Jan 26, 2025
1 parent 77b6b39 commit fd1647a
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 7 deletions.
27 changes: 23 additions & 4 deletions src/tools/days-calculator/days-calculator.service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,17 @@ describe('days-calculator', () => {
type: 'public',
},
];
const totalDiff1 = {
totalDifference: {
days: 30.416666666666668,
hours: 730,
minutes: 43800,
months: 0.9811827956989247,
seconds: 2628000,
weeks: 4.345238095238095,
years: 0.08333333333333333,
},
};

const date1 = new Date('2024-08-01T07:21:46Z');
const date2 = new Date('2024-08-31T17:21:46Z');
Expand All @@ -88,8 +99,8 @@ describe('days-calculator', () => {
businessSecondsFormatted: '11d 5h 38m 14s',
differenceFormatted: '29d 10h',
differenceSeconds: 2541600,
...totalDiff1,
totalDifferenceFormatted: '30d 10h',
totalDifferenceSeconds: 2628000,
holidays,
...daysInfos,
});
Expand All @@ -112,8 +123,16 @@ describe('days-calculator', () => {
businessSecondsFormatted: '10d 20h 38m 14.9s',
differenceFormatted: '28d 16h 38m 13.9s',
differenceSeconds: 2479093.999,
totalDifference: {
days: 29.69321758101852,
hours: 712.6372219444445,
minutes: 42758.233316666665,
months: 0.9578457284199522,
seconds: 2565493.999,
weeks: 4.241888225859788,
years: 0.08135128104388635,
},
totalDifferenceFormatted: '29d 16h 38m 13.9s',
totalDifferenceSeconds: 2565493.999,
holidays,
...daysInfos,
saturdays: [
Expand Down Expand Up @@ -142,8 +161,8 @@ describe('days-calculator', () => {
businessSecondsFormatted: '8d 5h 38m 14s',
differenceFormatted: '21d 14h 38m 14s',
differenceSeconds: 1867094,
...totalDiff1,
totalDifferenceFormatted: '30d 10h',
totalDifferenceSeconds: 2628000,
holidays,
...daysInfos,
});
Expand All @@ -167,7 +186,7 @@ describe('days-calculator', () => {
differenceFormatted: '4d',
differenceSeconds: 345600,
totalDifferenceFormatted: '30d 10h',
totalDifferenceSeconds: 2628000,
...totalDiff1,
holidays,
...daysInfos,
});
Expand Down
26 changes: 24 additions & 2 deletions src/tools/days-calculator/days-calculator.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,15 @@ import { BusinessTime, type Holiday } from './business-time-calculator';
interface DateTimeRange {
startDate: Date
endDate: Date
totalDifferenceSeconds: number
totalDifference: {
years: number
months: number
weeks: number
days: number
hours: number
minutes: number
seconds: number
}
totalDifferenceFormatted: string
differenceSeconds: number
differenceFormatted: string
Expand Down Expand Up @@ -96,14 +104,28 @@ export function diffDateTimes({
const startEnd = { start: startDateTime, end: endDateTime };

const totalDifferenceSeconds = endDateTime.diff(startDateTime, 'seconds').toObject().seconds || 0;
const totalDifferenceMinutes = endDateTime.diff(startDateTime, 'minutes').toObject().minutes || 0;
const totalDifferenceHours = endDateTime.diff(startDateTime, 'hours').toObject().hours || 0;
const totalDifferenceDays = endDateTime.diff(startDateTime, 'days').toObject().days || 0;
const totalDifferenceWeeks = endDateTime.diff(startDateTime, 'weeks').toObject().weeks || 0;
const totalDifferenceMonths = endDateTime.diff(startDateTime, 'months').toObject().months || 0;
const totalDifferenceYears = endDateTime.diff(startDateTime, 'years').toObject().years || 0;
const differenceSeconds = differenceTimeComputer.computeBusinessSecondsInInterval(startEnd);
const businessSeconds = businessTimeComputer.computeBusinessSecondsInInterval(startEnd);
const weekDaysDates = datesByDays(startDateTime, endDateTime);
const weekendDays = countCertainDays([6, 0], date1, date2);
return {
startDate: startDateTime.toJSDate(),
endDate: endDateTime.toJSDate(),
totalDifferenceSeconds,
totalDifference: {
years: totalDifferenceYears,
months: totalDifferenceMonths,
weeks: totalDifferenceWeeks,
days: totalDifferenceDays,
hours: totalDifferenceHours,
minutes: totalDifferenceMinutes,
seconds: totalDifferenceSeconds,
},
totalDifferenceFormatted: prettyMilliseconds(totalDifferenceSeconds * 1000),
differenceSeconds,
differenceFormatted: prettyMilliseconds(differenceSeconds * 1000),
Expand Down
12 changes: 11 additions & 1 deletion src/tools/days-calculator/days-calculator.vue
Original file line number Diff line number Diff line change
Expand Up @@ -146,21 +146,31 @@ const inputProps = {
<input-copyable v-bind="inputProps" label="End Date" :value="resultDaysDiff.endDate" />
<input-copyable v-bind="inputProps" label="End Date (ISO)" :value="resultDaysDiff.endDate.toISOString()" />
<n-divider />
<input-copyable v-bind="inputProps" label="Total Difference Seconds" :value="resultDaysDiff.totalDifferenceSeconds" />
<input-copyable v-bind="inputProps" label="Total Difference Seconds" :value="resultDaysDiff.totalDifference.seconds" />
<input-copyable v-bind="inputProps" label="Total Difference Minutes" :value="resultDaysDiff.totalDifference.minutes" />
<input-copyable v-bind="inputProps" label="Total Difference Hours" :value="resultDaysDiff.totalDifference.hours" />
<input-copyable v-bind="inputProps" label="Total Difference Days" :value="resultDaysDiff.totalDifference.days" />
<input-copyable v-bind="inputProps" label="Total Difference Weeks" :value="resultDaysDiff.totalDifference.weeks" />
<input-copyable v-bind="inputProps" label="Total Difference Months" :value="resultDaysDiff.totalDifference.months" />
<input-copyable v-bind="inputProps" label="Total Difference Years" :value="resultDaysDiff.totalDifference.years" />
<input-copyable v-bind="inputProps" label="Total Difference" :value="resultDaysDiff.totalDifferenceFormatted" />
<n-divider />
<input-copyable v-bind="inputProps" label="Difference Seconds" :value="resultDaysDiff.differenceSeconds" />
<input-copyable v-bind="inputProps" label="Difference " :value="resultDaysDiff.differenceFormatted" />
<n-divider />
<input-copyable v-bind="inputProps" label="Business Seconds" :value="resultDaysDiff.businessSeconds" />
<input-copyable v-bind="inputProps" label="Business Time" :value="resultDaysDiff.businessSecondsFormatted" />
<input-copyable v-bind="inputProps" label="Business Hours" :value="resultDaysDiff.businessHours" />
<input-copyable v-bind="inputProps" label="Business Days" :value="resultDaysDiff.businessDays" />
<n-divider />
<input-copyable v-bind="inputProps" placeholder="None" label="Mondays" :value="resultDaysDiff.mondays" />
<input-copyable v-bind="inputProps" placeholder="None" label="Tuesdays" :value="resultDaysDiff.tuesdays" />
<input-copyable v-bind="inputProps" placeholder="None" label="Wednesdays" :value="resultDaysDiff.wednesdays" />
<input-copyable v-bind="inputProps" placeholder="None" label="Thursdays" :value="resultDaysDiff.thursdays" />
<input-copyable v-bind="inputProps" placeholder="None" label="Fridays" :value="resultDaysDiff.fridays" />
<input-copyable v-bind="inputProps" placeholder="None" label="Saturdays" :value="resultDaysDiff.saturdays" />
<input-copyable v-bind="inputProps" placeholder="None" label="Sundays" :value="resultDaysDiff.sundays" />
<n-divider />
<input-copyable v-bind="inputProps" label="Weekend Days" :value="resultDaysDiff.weekendDays" />
<input-copyable v-bind="inputProps" label="Full Weekends" :value="resultDaysDiff.weekends" />
<c-card v-if="resultDaysDiff.holidays?.length" title="Holidays in period">
Expand Down

0 comments on commit fd1647a

Please sign in to comment.