Skip to content

Commit

Permalink
fix(datepicker): NgbCalendarIslamicUmalqura.fromGregorian with date i…
Browse files Browse the repository at this point in the history
…n the afternoon (#3256)

NgbCalendarIslamicUmalqura.fromGregorian returned a date one day after the
correct date for dates containing a time in the afternoon.

Fixes #3237
  • Loading branch information
divdavem authored and Benoit Charbonnier committed Jul 15, 2019
1 parent c6ad5b7 commit cc29290
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
10 changes: 10 additions & 0 deletions src/datepicker/hijri/ngb-calendar-islamic-umalqura.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -868,6 +868,16 @@ describe('ngb-calendar-islamic-umalqura', () => {
.toBeTruthy(`Gregorian ${gDate} should be Hijri ${iDate.year}-${iDate.month}-${iDate.day}`);
});
});

it('should convert correctly from Gregorian to Hijri with time 23:59:59.999', () => {
DATE_TABLE.forEach(element => {
const iDate = new NgbDate(element[3], element[4], element[5]);
const gDate = new Date(element[0], element[1], element[2], 23, 59, 59, 999);
const iDate2 = calendar.fromGregorian(gDate);
expect(iDate2.equals(iDate))
.toBeTruthy(`Gregorian ${gDate} should be Hijri ${iDate.year}-${iDate.month}-${iDate.day}`);
});
});
});

it('should return number of days per week', () => { expect(calendar.getDaysPerWeek()).toBe(7); });
Expand Down
5 changes: 4 additions & 1 deletion src/datepicker/hijri/ngb-calendar-islamic-umalqura.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,10 @@ const MONTH_LENGTH = [
];

function getDaysDiff(date1: Date, date2: Date): number {
const diff = Math.abs(date1.getTime() - date2.getTime());
// Ignores the time part in date1 and date2:
const time1 = Date.UTC(date1.getFullYear(), date1.getMonth(), date1.getDate());
const time2 = Date.UTC(date2.getFullYear(), date2.getMonth(), date2.getDate());
const diff = Math.abs(time1 - time2);
return Math.round(diff / ONE_DAY);
}

Expand Down

0 comments on commit cc29290

Please sign in to comment.