diff --git a/src/utils/common-expression-helpers.test.ts b/src/utils/common-expression-helpers.test.ts index 0397b19a..c2f126f6 100644 --- a/src/utils/common-expression-helpers.test.ts +++ b/src/utils/common-expression-helpers.test.ts @@ -171,7 +171,7 @@ describe('CommonExpressionHelpers', () => { it('should return the correct number of months on ART', () => { const artStartDate = new Date('2020-01-01'); const today = new Date(); - const monthsOnART = Math.floor((today.getTime() - artStartDate.getTime()) / (1000 * 60 * 60 * 24 * 30)); + const monthsOnART = dayjs(today).diff(dayjs(artStartDate), 'months'); expect(helpers.calcMonthsOnART(artStartDate)).toBe(monthsOnART); }); diff --git a/src/utils/common-expression-helpers.ts b/src/utils/common-expression-helpers.ts index caf6df9c..c4a4b64d 100644 --- a/src/utils/common-expression-helpers.ts +++ b/src/utils/common-expression-helpers.ts @@ -118,19 +118,22 @@ export class CommonExpressionHelpers { }; calcMonthsOnART = (artStartDate: Date) => { - if (artStartDate == null) return null; + if (artStartDate == null) { + return null; + } if (!(artStartDate instanceof Date)) { throw new Error('DateFormatException: value passed is not a valid date'); } - let today = new Date(); - let resultMonthsOnART: number; - let artInDays = Math.round((today.getTime() - artStartDate.getTime?.()) / 86400000); - if (artStartDate && artInDays >= 30) { - resultMonthsOnART = Math.floor(artInDays / 30); + const today = new Date(); + const artInDays = Math.round((today.getTime() - artStartDate.getTime()) / 86400000); + + if (artInDays < 30) { + return 0; } - return artStartDate ? resultMonthsOnART : null; + + return dayjs(today).diff(artStartDate, 'month'); }; calcViralLoadStatus = (viralLoadCount: number) => {