Skip to content

Instantly share code, notes, and snippets.

@marlonlom
Forked from RienNeVaPlus/dateDiff.ts
Last active August 30, 2022 03:32
Show Gist options
  • Save marlonlom/17482010164d3f3c756f81f500ffccd6 to your computer and use it in GitHub Desktop.
Save marlonlom/17482010164d3f3c756f81f500ffccd6 to your computer and use it in GitHub Desktop.
🕤 dateDiff() - returns a detail object about the difference between two dates
describe('date-diff', () => {
it('Should check if no date diff results shown for comparing exact same dates', () => {
const date0 = new Date('2020-11-01T01:00:00');
const date1 = new Date('2020-11-01T01:00:00');
const expectedResult = {
millennium: 0,
century: 0,
decade: 0,
year: 0,
quarter: 0,
month: 0,
week: 0,
day: 0,
hour: 0,
minute: 0,
second: 0,
millisecond: 0,
};
const dateDiffResult = dateDiff(date0, date1);
expect(dateDiffResult).toEqual(expectedResult);
});
it('Should check if date diff results contains 1 millennium for comparing dates', () => {
const date0 = new Date('1017-11-21T04:04:05');
const date1 = new Date('2022-07-01T00:50:11');
const dateDiffResult = dateDiff(date0, date1);
expect(dateDiffResult).toEqual(
jasmine.objectContaining({
millennium: 1,
year: 5,
quarter: 1,
week: 1,
day: 3,
})
);
});
it('Should check if date diff results contains 1 century for comparing dates', () => {
const date0 = new Date('1910-01-01T04:04:05');
const date1 = new Date('2022-09-01T06:50:10');
const dateDiffResult = dateDiff(date0, date1);
expect(dateDiffResult).toEqual(
jasmine.objectContaining({
century: 1,
decade: 1,
year: 2,
quarter: 3,
day: 1,
})
);
});
it('Should check if date diff results contains 2 decades for comparing dates', () => {
const date0 = new Date('2001-01-01T00:00:05');
const date1 = new Date('2022-08-01T01:00:10');
const dateDiffResult = dateDiff(date0, date1);
expect(dateDiffResult).toEqual(
jasmine.objectContaining({
decade: 2,
year: 1,
quarter: 2,
month: 1,
week: 1,
day: 0,
hour: 1,
})
);
});
it('Should check if date diff results contains 5 years for comparing dates', () => {
const date0 = new Date('2017-05-01T00:00:00');
const date1 = new Date('2022-08-03T01:06:00');
const dateDiffResult = dateDiff(date0, date1);
expect(dateDiffResult).toEqual(
jasmine.objectContaining({
year: 5,
quarter: 1,
day: 5,
hour: 1,
minute: 6,
})
);
});
it('Should check if date diff results contains 1 quarter for comparing dates', () => {
const date0 = new Date('2022-05-01T00:00:00');
const date1 = new Date('2022-08-08T00:00:00');
const dateDiffResult = dateDiff(date0, date1);
expect(dateDiffResult).toEqual(
jasmine.objectContaining({
quarter: 1,
week: 1,
day: 2,
})
);
});
it('Should check if date diff results contains 2 months for comparing dates', () => {
const date0 = new Date('2022-05-01T00:00:00');
const date1 = new Date('2022-07-03T00:00:00');
const dateDiffResult = dateDiff(date0, date1);
expect(dateDiffResult).toEqual(jasmine.objectContaining({ month: 2 }));
});
it('Should check if date diff results contains 3 weeks and one day for comparing dates', () => {
const date0 = new Date('2022-07-31T00:06:00');
const date1 = new Date('2022-08-22T00:07:00');
const dateDiffResult = dateDiff(date0, date1);
expect(dateDiffResult).toEqual(jasmine.objectContaining({ week: 3, day: 1 }));
});
it('Should check if date diff results contains 4 days for comparing dates', () => {
const date0 = new Date('1990-02-10T00:24:00');
const date1 = new Date('1990-02-14T01:24:00');
const dateDiffResult = dateDiff(date0, date1);
expect(dateDiffResult).toEqual(jasmine.objectContaining({ day: 4 }));
});
it('Should check if date diff results contains 2 hours for comparing dates', () => {
const date0 = new Date('1990-11-10T03:24:00');
const date1 = new Date('1990-11-10T05:24:00');
const dateDiffResult = dateDiff(date0, date1);
expect(dateDiffResult).toEqual(jasmine.objectContaining({ hour: 2 }));
});
it('Should check if date diff results contains 30 minutes for comparing dates', () => {
const date0 = new Date('1995-12-17T03:24:00');
const date1 = new Date('1995-12-17T03:54:00');
const dateDiffResult = dateDiff(date0, date1);
expect(dateDiffResult).toEqual(jasmine.objectContaining({ minute: 30 }));
});
it('Should check if date diff results contains 45 seconds for comparing dates', () => {
const date0 = new Date('2000-12-17T03:00:00');
const date1 = new Date('2000-12-17T03:00:45');
const dateDiffResult = dateDiff(date0, date1);
expect(dateDiffResult).toEqual(jasmine.objectContaining({ second: 45 }));
});
it('Should check if date diff results contains 1 millisecond for comparing dates, when initial date is before final date', () => {
const date0 = new Date(1661817326554);
const date1 = new Date(1661817326555);
const dateDiffResult = dateDiff(date0, date1);
expect(dateDiffResult).toEqual(jasmine.objectContaining({ millisecond: 1 }));
});
it('Should check if date diff results contains 1 millisecond for comparing dates, when initial date is after final date', () => {
const date0 = new Date(1661817326555);
const date1 = new Date(1661817326554);
const dateDiffResult = dateDiff(date0, date1);
expect(dateDiffResult).toEqual(jasmine.objectContaining({ millisecond: 1 }));
});
});
const DATE_DIFF_DEF = Object.freeze({
millennium: 31536000000000,
century: 3153600000000,
decade: 315360000000,
year: 31536000000,
quarter: 7776000000,
month: 2592000000,
week: 604800000,
day: 86400000,
hour: 3600000,
minute: 60000,
second: 1000,
millisecond: 1,
});
export type DateDiffKey = keyof typeof DATE_DIFF_DEF;
export type DateDifference = Record<DateDiffKey, number>;
const DATE_DIFF_KEYS = Object.freeze(Object.keys(DATE_DIFF_DEF)) as DateDiffKey[];
/**
* ☃ dateDiff "Snowman Carl" (http://stackoverflow.com/questions/13903897)
* Returns a detail object about the difference between two dates
*
* When providing custom units, provide them in descending order (eg week,day,hour; not hour,day,week)
*
* @param {Date} dateStart - date to compare to
* @param {Date} dateEnd - second date, can be used as unit param instead
* @returns {DateDifference }
*/
export function dateDiff(dateStart: Date, dateEnd: Date): DateDifference {
let delta: number = Math.abs(dateStart.getTime() - dateEnd.getTime());
return DATE_DIFF_KEYS.reduce<DateDifference>((res: DateDifference, key: DateDiffKey) => {
res[key] = Math.floor(delta / DATE_DIFF_DEF[key]);
delta -= res[key] * DATE_DIFF_DEF[key];
return res;
}, {} as DateDifference);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment