Skip to content

Commit 9a51a2c

Browse files
committed
fix(datepicker): native date adapter not preserving time when cloning
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.
1 parent d22f48c commit 9a51a2c

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

src/lib/core/datetime/native-date-adapter.spec.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,12 @@ describe('NativeDateAdapter', () => {
290290
expect(adapter.clone(date)).not.toBe(date);
291291
});
292292

293+
it('should preserve time when cloning', () => {
294+
let date = new Date(2017, JAN, 1, 4, 5, 6);
295+
expect(adapter.clone(date)).toEqual(date);
296+
expect(adapter.clone(date)).not.toBe(date);
297+
});
298+
293299
it('should compare dates', () => {
294300
expect(adapter.compareDate(new Date(2017, JAN, 1), new Date(2017, JAN, 2))).toBeLessThan(0);
295301
expect(adapter.compareDate(new Date(2017, JAN, 1), new Date(2017, FEB, 1))).toBeLessThan(0);

src/lib/core/datetime/native-date-adapter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ export class NativeDateAdapter extends DateAdapter<Date> {
146146
}
147147

148148
clone(date: Date): Date {
149-
return this.createDate(this.getYear(date), this.getMonth(date), this.getDate(date));
149+
return new Date(date.getTime());
150150
}
151151

152152
createDate(year: number, month: number, date: number): Date {

0 commit comments

Comments
 (0)