Skip to content

Commit

Permalink
fix(datepicker): native date adapter not preserving time when cloning (
Browse files Browse the repository at this point in the history
…#14691)

When the `NativeDateAdapter` clones a date, it creates the clone only from the year, month and day which means that the clone's time will be different from the source.
  • Loading branch information
crisbeto authored and mmalerba committed Jan 4, 2019
1 parent 17bb9c3 commit 9e3a77b
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/lib/core/datetime/native-date-adapter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,12 @@ describe('NativeDateAdapter', () => {
expect(adapter.clone(date)).not.toBe(date);
});

it('should preserve time when cloning', () => {
let date = new Date(2017, JAN, 1, 4, 5, 6);
expect(adapter.clone(date)).toEqual(date);
expect(adapter.clone(date)).not.toBe(date);
});

it('should compare dates', () => {
expect(adapter.compareDate(new Date(2017, JAN, 1), new Date(2017, JAN, 2))).toBeLessThan(0);
expect(adapter.compareDate(new Date(2017, JAN, 1), new Date(2017, FEB, 1))).toBeLessThan(0);
Expand Down
2 changes: 1 addition & 1 deletion src/lib/core/datetime/native-date-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ export class NativeDateAdapter extends DateAdapter<Date> {
}

clone(date: Date): Date {
return this.createDate(this.getYear(date), this.getMonth(date), this.getDate(date));
return new Date(date.getTime());
}

createDate(year: number, month: number, date: number): Date {
Expand Down

0 comments on commit 9e3a77b

Please sign in to comment.