@@ -34,6 +34,11 @@ export interface CalendarDateRange {
34
34
endDate : Date | undefined ;
35
35
}
36
36
37
+ interface CalendarDateRangeSelection {
38
+ startDateSelection : boolean ;
39
+ endDateSelection : boolean ;
40
+ }
41
+
37
42
@Component ( {
38
43
selector : 'ngx-calendar' ,
39
44
exportAs : 'ngxCalendar' ,
@@ -61,7 +66,10 @@ export class CalendarComponent implements OnInit, AfterViewInit, ControlValueAcc
61
66
@Input ( ) inputFormats : Array < string | MomentBuiltinFormat > = [ 'L' , 'LT' , 'L LT' , moment . ISO_8601 ] ;
62
67
@Input ( ) selectType : string = CalendarSelect . Single ;
63
68
@Input ( ) dateLabelFormat : string = 'MMM D YYYY' ;
64
- @Input ( ) range : CalendarDateRange = { startDate : undefined , endDate : undefined } ;
69
+ @Input ( ) range : CalendarDateRange = {
70
+ startDate : undefined ,
71
+ endDate : undefined
72
+ } ;
65
73
66
74
@Input ( 'minView' )
67
75
get minView ( ) {
@@ -114,12 +122,16 @@ export class CalendarComponent implements OnInit, AfterViewInit, ControlValueAcc
114
122
monthsList = moment . monthsShort ( ) ;
115
123
startYear : number ;
116
124
117
- startHour : number ;
118
- endHour : number ;
119
- startMinute : number ;
120
- endMinute : number ;
121
- startAmPmVal : string ;
122
- endAmPmVal : string ;
125
+ startHour : string ;
126
+ endHour : string ;
127
+ startMinute : string ;
128
+ endMinute : string ;
129
+ startAmPmVal = 'AM' ;
130
+ endAmPmVal = 'AM' ;
131
+ dateRangeSelection : CalendarDateRangeSelection = {
132
+ startDateSelection : false ,
133
+ endDateSelection : false
134
+ } ;
123
135
124
136
readonly CalendarView = CalendarView ;
125
137
readonly CalendarSelect = CalendarSelect ;
@@ -153,7 +165,6 @@ export class CalendarComponent implements OnInit, AfterViewInit, ControlValueAcc
153
165
} else {
154
166
this . currentView = this . minView ;
155
167
}
156
-
157
168
this . weeks = getMonth ( this . focusDate ) ;
158
169
}
159
170
@@ -177,27 +188,23 @@ export class CalendarComponent implements OnInit, AfterViewInit, ControlValueAcc
177
188
* Initializes the values for initial time
178
189
*/
179
190
initializeTime ( ) : void {
180
- this . startHour = this . range . startDate
181
- ? this . range . startDate . getHours ( ) % 12
182
- : + moment ( '2001-01-01T00:00:00' ) . format ( 'hh' ) % 12 ;
183
- this . endHour = this . range . endDate
184
- ? this . range . endDate . getHours ( ) % 12
185
- : + moment ( '2001-01-01T00:00:00' ) . format ( 'hh' ) % 12 ;
186
-
187
- this . startMinute = this . range . startDate
188
- ? this . range . startDate . getMinutes ( )
189
- : + moment ( '2001-01-01T00:00:00' ) . format ( 'mm' ) ;
190
- this . endMinute = this . range . endDate ? this . range . endDate . getMinutes ( ) : + moment ( '2001-01-01T00:00:00' ) . format ( 'mm' ) ;
191
- this . startAmPmVal = this . range . startDate
192
- ? this . range . startDate . getHours ( ) >= 12
193
- ? 'PM'
194
- : 'AM'
195
- : moment ( '2001-01-01T00:00:00' ) . format ( 'A' ) ;
196
- this . endAmPmVal = this . range . endDate
197
- ? this . range . endDate . getHours ( ) >= 12
198
- ? 'PM'
199
- : 'AM'
200
- : moment ( '2001-01-01T00:00:00' ) . format ( 'A' ) ;
191
+ let startDate : Date | undefined , endDate : Date | undefined ;
192
+ if ( ! this . range . startDate ) {
193
+ startDate = new Date ( new Date ( ) . setHours ( 0 , 0 , 0 , 0 ) ) ;
194
+ } else {
195
+ startDate = this . range . startDate ;
196
+ }
197
+ if ( ! this . range . endDate ) {
198
+ endDate = new Date ( new Date ( ) . setHours ( 0 , 0 , 0 , 0 ) ) ;
199
+ } else {
200
+ endDate = this . range . endDate ;
201
+ }
202
+ this . startHour = moment ( startDate ) . format ( 'hh' ) ;
203
+ this . startMinute = moment ( startDate ) . format ( 'mm' ) ;
204
+ this . startAmPmVal = moment ( startDate ) . format ( 'a' ) . toUpperCase ( ) ;
205
+ this . endHour = moment ( endDate ) . format ( 'hh' ) ;
206
+ this . endMinute = moment ( endDate ) . format ( 'mm' ) ;
207
+ this . endAmPmVal = moment ( endDate ) . format ( 'a' ) . toUpperCase ( ) ;
201
208
}
202
209
203
210
/**
@@ -324,24 +331,35 @@ export class CalendarComponent implements OnInit, AfterViewInit, ControlValueAcc
324
331
325
332
if ( this . range . startDate === undefined && this . range . endDate === undefined ) {
326
333
this . range . startDate = this . focusDate . toDate ( ) ;
327
- this . range . startDate . setHours ( this . startHour ) ;
328
- this . range . startDate . setMinutes ( + this . startMinute ) ;
334
+ this . range . startDate . setHours ( Number ( this . startHour ) ) ;
335
+ this . range . startDate . setMinutes ( Number ( this . startMinute ) ) ;
336
+ this . dateRangeSelection . startDateSelection = true ;
329
337
} else if ( this . range . endDate === undefined ) {
330
338
if ( this . focusDate . toDate ( ) > this . range . startDate ) {
331
339
this . range . endDate = this . focusDate . toDate ( ) ;
332
- this . range . endDate . setHours ( this . endHour ) ;
333
- this . range . endDate . setMinutes ( + this . endMinute ) ;
340
+ this . range . endDate . setHours ( Number ( this . endHour ) ) ;
341
+ this . range . endDate . setMinutes ( Number ( this . endMinute ) ) ;
334
342
} else {
335
343
this . range . startDate = this . focusDate . toDate ( ) ;
336
- this . range . startDate . setHours ( this . startHour ) ;
337
- this . range . startDate . setMinutes ( + this . startMinute ) ;
344
+ this . range . startDate . setHours ( Number ( this . startHour ) ) ;
345
+ this . range . startDate . setMinutes ( Number ( this . startMinute ) ) ;
346
+ this . dateRangeSelection . startDateSelection = true ;
338
347
}
339
348
} else {
340
349
this . range . startDate = this . focusDate . toDate ( ) ;
341
- this . range . startDate . setHours ( this . startHour ) ;
342
- this . range . startDate . setMinutes ( + this . startMinute ) ;
350
+ this . range . startDate . setHours ( Number ( this . startHour ) ) ;
351
+ this . range . startDate . setMinutes ( Number ( this . startMinute ) ) ;
352
+ this . dateRangeSelection . endDateSelection = false ;
353
+ this . range . endDate = undefined ;
354
+ }
355
+
356
+ if ( this . dateRangeSelection . startDateSelection && this . dateRangeSelection . endDateSelection ) {
357
+ this . dateRangeSelection . startDateSelection = false ;
358
+ this . dateRangeSelection . endDateSelection = false ;
359
+ this . range . startDate = undefined ;
343
360
this . range . endDate = undefined ;
344
361
}
362
+
345
363
this . onRangeSelect . emit ( { startDate : this . range . startDate , endDate : this . range . endDate } ) ;
346
364
347
365
if ( day . prevMonth || day . nextMonth ) {
@@ -375,52 +393,59 @@ export class CalendarComponent implements OnInit, AfterViewInit, ControlValueAcc
375
393
}
376
394
}
377
395
378
- hourChanged ( newVal : number , type : string ) {
379
- newVal = + newVal % 12 ;
396
+ hourChanged ( newVal : string , type : string ) {
380
397
if ( type === 'start' ) {
381
398
if ( this . range . startDate ) {
382
- if ( this . startAmPmVal === 'PM' ) newVal = 12 + newVal ;
383
- this . range . startDate . setHours ( newVal ) ;
399
+ if ( this . endAmPmVal === 'PM' ) {
400
+ this . range . startDate . setHours ( 12 + Number ( newVal ) ) ;
401
+ } else {
402
+ this . range . startDate . setHours ( Number ( newVal ) ) ;
403
+ }
384
404
}
385
- this . startHour = newVal % 12 ;
405
+ this . startHour = newVal ;
386
406
} else {
387
407
if ( this . range . endDate ) {
388
- if ( this . endAmPmVal === 'PM' ) newVal = 12 + newVal ;
389
- this . range . endDate . setHours ( newVal ) ;
408
+ if ( this . endAmPmVal === 'PM' ) {
409
+ this . range . endDate . setHours ( 12 + Number ( newVal ) ) ;
410
+ } else {
411
+ this . range . endDate . setHours ( Number ( newVal ) ) ;
412
+ }
390
413
}
391
- this . endHour = newVal % 12 ;
414
+ this . endHour = newVal ;
392
415
}
393
416
this . onRangeSelect . emit ( { startDate : this . range . startDate , endDate : this . range . endDate } ) ;
394
417
}
395
- minuteChanged ( newVal : number , type : string ) {
418
+ minuteChanged ( newVal : string , type : string ) {
396
419
if ( type === 'start' ) {
397
- if ( this . range . startDate ) this . range . startDate . setMinutes ( newVal ) ;
420
+ if ( this . range . startDate ) {
421
+ this . range . startDate . setMinutes ( Number ( newVal ) ) ;
422
+ }
398
423
this . startMinute = newVal ;
399
424
} else {
400
- if ( this . range . endDate ) this . range . endDate . setMinutes ( newVal ) ;
425
+ if ( this . range . endDate ) {
426
+ this . range . endDate . setMinutes ( Number ( newVal ) ) ;
427
+ }
401
428
this . endMinute = newVal ;
402
429
}
403
430
this . onRangeSelect . emit ( { startDate : this . range . startDate , endDate : this . range . endDate } ) ;
404
431
}
405
432
onAmPmChange ( newVal , type ) {
406
433
if ( type === 'start' ) {
407
- if ( this . range . startDate ) {
408
- const hourClone = this . range . startDate . getHours ( ) ;
409
- if ( newVal === 'AM' && this . startAmPmVal === 'PM' ) {
410
- this . range . startDate . setHours ( hourClone - 12 ) ;
411
- } else if ( newVal === 'PM' && this . startAmPmVal === 'AM' ) {
412
- this . range . startDate . setHours ( hourClone + 12 ) ;
413
- }
434
+ const hourClone = this . range . startDate . getHours ( ) ;
435
+ if ( hourClone >= 12 && newVal === 'AM' ) {
436
+ this . range . startDate . setHours ( hourClone - 12 ) ;
437
+ }
438
+ if ( hourClone >= 0 && hourClone < 12 && newVal === 'PM' ) {
439
+ this . range . startDate . setHours ( hourClone + 12 ) ;
414
440
}
415
441
this . startAmPmVal = newVal ;
416
442
} else {
417
- if ( this . range . endDate ) {
418
- const hourClone = this . range . endDate . getHours ( ) ;
419
- if ( newVal === 'AM' && this . endAmPmVal === 'PM' ) {
420
- this . range . endDate . setHours ( hourClone - 12 ) ;
421
- } else if ( newVal === 'PM' && this . endAmPmVal === 'AM' ) {
422
- this . range . endDate . setHours ( hourClone + 12 ) ;
423
- }
443
+ const hourClone = this . range . endDate . getHours ( ) ;
444
+ if ( hourClone >= 12 && newVal === 'AM' ) {
445
+ this . range . endDate . setHours ( hourClone - 12 ) ;
446
+ }
447
+ if ( hourClone >= 0 && hourClone < 12 && newVal === 'PM' ) {
448
+ this . range . endDate . setHours ( hourClone + 12 ) ;
424
449
}
425
450
this . endAmPmVal = newVal ;
426
451
}
@@ -508,8 +533,6 @@ export class CalendarComponent implements OnInit, AfterViewInit, ControlValueAcc
508
533
}
509
534
510
535
onDayDown ( event : KeyboardEvent ) {
511
- // console.log(event.code);
512
-
513
536
let stop = false ;
514
537
515
538
if ( this . currentView === CalendarView . Date ) {
0 commit comments