Skip to content

Commit 39e7da3

Browse files
committed
fix(datetime): set default to max if max before current only #9846
1 parent 9b3fb78 commit 39e7da3

File tree

3 files changed

+27
-3
lines changed

3 files changed

+27
-3
lines changed

src/components/datetime/datetime.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { assert, clamp, deepCopy, isArray, isBlank, isObject, isPresent, isStrin
1212
import {
1313
DateTimeData,
1414
LocaleData,
15+
compareDates,
1516
convertDataToISO,
1617
convertFormatToKey,
1718
dateDataSortValue,
@@ -850,10 +851,17 @@ export class DateTime extends BaseInput<DateTimeData> implements AfterContentIni
850851
* @private
851852
*/
852853
getDefaultValue() {
854+
const now = parseDate((new Date).toISOString());
853855
if (this.max) {
854-
return parseDate(this.max);
856+
const max = parseDate(this.max);
857+
const diff = compareDates(now, max);
858+
859+
// If max is before current time, return max
860+
if (diff > 0) {
861+
return max;
862+
}
855863
}
856-
return parseDate((new Date).toISOString());
864+
return now;
857865
}
858866
}
859867

src/components/datetime/test/datetime.spec.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ describe('DateTime', () => {
2525
expect(val.minute).toEqual(now.minute);
2626
});
2727

28-
fit('should default to max if no initial value supplied but max specified', () => {
28+
fit('should default to max if no initial value supplied but max specified and max before current', () => {
2929
datetime.max = '1987-10-19';
3030
datetime.generate();
3131
const val = datetime.getValue();
@@ -34,6 +34,16 @@ describe('DateTime', () => {
3434
expect(val.day).toEqual(19);
3535
});
3636

37+
fit('should default to current if no initial value supplied but max specified and max after current', () => {
38+
const now = datetimeUtil.parseDate(new Date().toISOString());
39+
datetime.max = '2100-10-19';
40+
datetime.generate();
41+
const val = datetime.getValue();
42+
expect(val.year).toEqual(now.year);
43+
expect(val.month).toEqual(now.month);
44+
expect(val.day).toEqual(now.day);
45+
});
46+
3747
it('should restrict January 1-14, 2000 from selection, then allow it, and restrict December 15-31, 2001', () => {
3848
datetime.max = '2001-12-15';
3949
datetime.min = '2000-01-15';

src/util/datetime-util.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,12 @@ export function parseDate(val: any): DateTimeData {
228228
};
229229
}
230230

231+
export function compareDates(d1: DateTimeData, d2: DateTimeData) : number {
232+
const date1 = new Date(d1.year, d1.month, d1.day, d1.hour, d1.minute, d1.second);
233+
const date2 = new Date(d2.year, d2.month, d2.day, d2.hour, d2.minute, d2.second);
234+
235+
return date1.getTime() - date2.getTime();
236+
}
231237

232238
export function updateDate(existingData: DateTimeData, newData: any): boolean {
233239
if (isPresent(newData) && newData !== '') {

0 commit comments

Comments
 (0)