primeng icon indicating copy to clipboard operation
primeng copied to clipboard

Calendar - time in minDate, maxDate is disregarded when showTime is enabled

Open bnazare opened this issue 9 years ago • 8 comments

I'm submitting a ... (check one with "x")

[?] bug report => Search github for a similar issue or PR before submitting
[?] feature request => Please check if request is not on the roadmap already https://github.com/primefaces/primeng/wiki/Roadmap
[ ] support request => Please do not submit support request here, instead see http://forum.primefaces.org/viewforum.php?f=35

Not sure if this is intended behaviour or not

Plunkr Case (Bug Reports) http://plnkr.co/edit/GlMQFaEPxKgphxaCyM6f?p=preview

Current behavior Whenever a calendar component is created with both showTime and minDate/maxDate options, days on the calendar are correctly blocked however it is till possible to select a moment (considering both date and time) under minDate or over maxDate.

Expected behavior For a calendar component created with both showTime and minDate/maxDate options it should not be possible no select a moment under minDate or over maxDate. As an alternative the date could selectable but somehow be marked as invalid.

Minimal reproduction of the problem with instructions

  1. Go to the plunkr provided above
  2. Notice that the dates used as min and max dates contain a time value (hours and minutes != 0)
  3. Attempt to select a date. Notice that selectable days are indeed locked accordingly to min and max date
  4. Select the first available day and attempt to select an early time
  5. Notice you can go below the min date

What is the motivation / use case for changing the behavior? When allowing selection of a date/time it is common to want to limit that selection to after or before another given date/time. An example: to limit the selectable values of an arrival date to after a departure date which may occur on the same day but at different times.

Please tell us about your environment:

  • Angular version: 2.0.X
  • Operating system: Linux Mint 18.1
  • IDE: Visual Studio Code
  • package manager: NPM
  • Angular version: 4.0.3
  • Angular CLI version: 1.0.3
  • PrimeNG version: 4.0.1
  • Browser: Chromium 58 | Firefox 53
  • Language: TypeScript 2.2

bnazare avatar May 25 '17 15:05 bnazare

Additionally, using the Time component at the bottom of the DatePicker, it is possible to interract with it if the minDate is set higher than today (= the default selected value). So it is possible to select "today", even if the mindate is tomorrow, by clicking any of the time controls.

Mysame avatar Sep 12 '17 14:09 Mysame

https://github.com/primefaces/primeng/blob/master/src/app/components/calendar/calendar.ts#L853

var selectedDate = !_showTime ?
new Date(year, month, day) :
new Date(year, month, day,  this.currentHour, this.currentMinute, this.currentSecond);

validMin = !this.minDate || this.minDate < selectedDate;
validMax = !this.maxDate || this.maxDate > selectedDate;

Step it up, Prime!

Mysame avatar Sep 27 '17 14:09 Mysame

The comparision should rather be so:

validMin = !this.minDate || this.minDate <= selectedDate; validMax = !this.maxDate || this.maxDate => selectedDate;

ghost avatar Sep 27 '17 15:09 ghost

Hi, I am facing an issue in primeNG@v[4.3.0]. I am not able to update the [minDate] attribute of p-calendar tag in DOM. Currently the scenario I am facing the issue in is as follows:-

I have two p-calendar tags in the DOM. The first one when clicked will call a function and set the [minDate] of the second p-calendar tag.

This does not work.

Please help.

lucifer-AM avatar Jan 03 '18 10:01 lucifer-AM

It's actually not fixed in 4.3 the validations that are added are not working cause they are checking only the minutes of the minDate and both the ValidateMinute and ValidateHour and the updateTime don't account for the defaultDate, which in turn calls onSelect so end up with a date with time that is today. Now i'm not sure how to proceed. Can i change the value after it was selected from outside ?gonna try but this is dirty hack @cagataycivici

talamaska avatar Oct 17 '18 16:10 talamaska

had to do a dirty hack with reactive forms and rxjs. Not sure how you provide this library without proper testing, this is not because of older version of angular.

talamaska avatar Oct 18 '18 12:10 talamaska

@talamaska , hey hi, I am kind of facing same issue. My scenario:

  1. Set the minDate and maxDate
  2. Go to any day between minDate and maxDate
  3. Change time in such a way it is beyond the time time in maxDate or early time for time in minDate
  4. Now go to min date / max date in calendar

The time control gets hanged, it does not allow to change time through arrows for hours, minutes and seconds are changing.

Any solution that you applied to reset that time or something like that for min and max dates?

ruchidhore12 avatar Jan 03 '19 06:01 ruchidhore12

@RuchiDhore something along the lines, I'm only covering the case for minDate

    this.expireSub = this.form.get('expires').valueChanges.pipe(
      distinctUntilChanged((oldDate: Date, newDate: Date) => {
        return oldDate.getHours() === newDate.getHours() || oldDate.getMinutes() === newDate.getMinutes();
      }),
      filter((data: Date) => {
        return data.getTime() < this.minDate.getTime();
      })
    ).subscribe((data: Date) => {
      // console.log(data);
      const hours = data.getHours();
      const minutes = data.getMinutes();
      const newDate = new Date(this.minDate.getFullYear(), this.minDate.getMonth(), this.minDate.getDate(), hours, minutes);
      this.form.get('expires').setValue(newDate);
    });
<p-calendar formControlName="expires" name="expires" [defaultDate]="minDate" [minDate]="minDate" [yearNavigator]="true" hourFormat="12" [showTime]="true" yearRange="2018:2030" [readonlyInput]="true" [showButtonBar]="true"></p-calendar>

talamaska avatar Jan 03 '19 12:01 talamaska