Commit 4dab7805 authored by Illya Klymov's avatar Illya Klymov

Merge branch '246492-mlunoe-add-difference-in-months-utility' into 'master'

Feat(datetime utility): add diff months util

See merge request gitlab-org/gitlab!45363
parents 68ec34f2 0e02203b
......@@ -744,6 +744,21 @@ export const differenceInSeconds = (startDate, endDate) => {
return (endDate.getTime() - startDate.getTime()) / 1000;
};
/**
* A utility function which computes the difference in months
* between 2 dates.
*
* @param {Date} startDate the start date
* @param {Date} endDate the end date
*
* @return {Int} the difference in months
*/
export const differenceInMonths = (startDate, endDate) => {
const yearDiff = endDate.getYear() - startDate.getYear();
const monthDiff = endDate.getMonth() - startDate.getMonth();
return monthDiff + 12 * yearDiff;
};
/**
* A utility function which computes the difference in milliseconds
* between 2 dates.
......
......@@ -682,6 +682,20 @@ describe('differenceInSeconds', () => {
});
});
describe('differenceInMonths', () => {
const startDateTime = new Date('2019-07-17T00:00:00.000Z');
it.each`
startDate | endDate | expected
${startDateTime} | ${startDateTime} | ${0}
${startDateTime} | ${new Date('2019-12-17T12:00:00.000Z')} | ${5}
${startDateTime} | ${new Date('2021-02-18T00:00:00.000Z')} | ${19}
${new Date('2021-02-18T00:00:00.000Z')} | ${startDateTime} | ${-19}
`('returns $expected for $endDate - $startDate', ({ startDate, endDate, expected }) => {
expect(datetimeUtility.differenceInMonths(startDate, endDate)).toBe(expected);
});
});
describe('differenceInMilliseconds', () => {
const startDateTime = new Date('2019-07-17T00:00:00.000Z');
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment