@@ -168,7 +168,17 @@ export class MatMonthView<D> implements AfterContentInit, OnDestroy {
168
168
169
169
/** Handles when a new date is selected. */
170
170
_dateSelected ( date : number ) {
171
- if ( this . _rangeStart !== date || this . _rangeEnd !== date ) {
171
+ let rangeStartDate : number | null ;
172
+ let rangeEndDate : number | null ;
173
+
174
+ if ( this . _selected instanceof DateRange ) {
175
+ rangeStartDate = this . _getDateInCurrentMonth ( this . _selected . start ) ;
176
+ rangeEndDate = this . _getDateInCurrentMonth ( this . _selected . end ) ;
177
+ } else {
178
+ rangeStartDate = rangeEndDate = this . _getDateInCurrentMonth ( this . _selected ) ;
179
+ }
180
+
181
+ if ( rangeStartDate !== date || rangeEndDate !== date ) {
172
182
const selectedYear = this . _dateAdapter . getYear ( this . activeDate ) ;
173
183
const selectedMonth = this . _dateAdapter . getMonth ( this . activeDate ) ;
174
184
const selectedDate = this . _dateAdapter . createDate ( selectedYear , selectedMonth , date ) ;
@@ -246,7 +256,7 @@ export class MatMonthView<D> implements AfterContentInit, OnDestroy {
246
256
/** Initializes this month view. */
247
257
_init ( ) {
248
258
this . _setRange ( this . selected ) ;
249
- this . _todayDate = this . _getDateInCurrentMonth ( this . _dateAdapter . today ( ) ) ;
259
+ this . _todayDate = this . _getCellCompareValue ( this . _dateAdapter . today ( ) ) ;
250
260
this . _monthLabel =
251
261
this . _dateAdapter . getMonthNames ( 'short' ) [ this . _dateAdapter . getMonth ( this . activeDate ) ]
252
262
. toLocaleUpperCase ( ) ;
@@ -297,8 +307,8 @@ export class MatMonthView<D> implements AfterContentInit, OnDestroy {
297
307
const ariaLabel = this . _dateAdapter . format ( date , this . _dateFormats . display . dateA11yLabel ) ;
298
308
const cellClasses = this . dateClass ? this . dateClass ( date ) : undefined ;
299
309
300
- this . _weeks [ this . _weeks . length - 1 ]
301
- . push ( new MatCalendarCell ( i + 1 , dateNames [ i ] , ariaLabel , enabled , cellClasses ) ) ;
310
+ this . _weeks [ this . _weeks . length - 1 ] . push ( new MatCalendarCell ( i + 1 , dateNames [ i ] , ariaLabel ,
311
+ enabled , cellClasses , this . _getCellCompareValue ( date ) ! ) ) ;
302
312
}
303
313
}
304
314
@@ -325,6 +335,20 @@ export class MatMonthView<D> implements AfterContentInit, OnDestroy {
325
335
this . _dateAdapter . getYear ( d1 ) == this . _dateAdapter . getYear ( d2 ) ) ;
326
336
}
327
337
338
+ /** Gets the value that will be used to one cell to another. */
339
+ private _getCellCompareValue ( date : D | null ) : number | null {
340
+ if ( date ) {
341
+ // We use the time since the Unix epoch to compare dates in this view, rather than the
342
+ // cell values, because we need to support ranges that span across multiple months/years.
343
+ const year = this . _dateAdapter . getYear ( date ) ;
344
+ const month = this . _dateAdapter . getMonth ( date ) ;
345
+ const day = this . _dateAdapter . getDate ( date ) ;
346
+ return new Date ( year , month , day ) . getTime ( ) ;
347
+ }
348
+
349
+ return null ;
350
+ }
351
+
328
352
/**
329
353
* @param obj The object to check.
330
354
* @returns The given object if it is both a date instance and valid, otherwise null.
@@ -341,10 +365,10 @@ export class MatMonthView<D> implements AfterContentInit, OnDestroy {
341
365
/** Sets the current range based on a model value. */
342
366
private _setRange ( value : DateRange < D > | D | null ) {
343
367
if ( value instanceof DateRange ) {
344
- this . _rangeStart = this . _getDateInCurrentMonth ( value . start ) ;
345
- this . _rangeEnd = this . _getDateInCurrentMonth ( value . end ) ;
368
+ this . _rangeStart = this . _getCellCompareValue ( value . start ) ;
369
+ this . _rangeEnd = this . _getCellCompareValue ( value . end ) ;
346
370
} else {
347
- this . _rangeStart = this . _rangeEnd = this . _getDateInCurrentMonth ( value ) ;
371
+ this . _rangeStart = this . _rangeEnd = this . _getCellCompareValue ( value ) ;
348
372
}
349
373
}
350
374
}
0 commit comments