Skip to content

Commit 9e3a77b

Browse files
crisbetommalerba
authored andcommittedJan 4, 2019
fix(datepicker): native date adapter not preserving time when cloning (#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.
1 parent 17bb9c3 commit 9e3a77b

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

+6
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

+1-1
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)
Please sign in to comment.