question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Min / Max too close seems not working

See original GitHub issue

Hello,

I’ve seen a weird behavior. It seems a bug. When I set a minDate and a maxDate too close, the visual picker don’t let met pick the correct hours. Can’t understand why. There is an example :

// example.component.ts
this.minDate = new Date('2021-04-12T17:50');
this.maxDate = new Date('2021-04-12T18:15');
// example.component.html
<mat-form-field>
          <input
            [formControl]="form.get('arrival.date')"
            matInput
            [matDatetimepicker]="arrival"
            required
            [min]="minDate"
            [max]="maxDate"
            [disabled]="form.get('arrival.date').disabled"
            autocomplete="false"
          />
          <mat-datetimepicker-toggle
            [for]="arrival"
            matPrefix
          ></mat-datetimepicker-toggle>
          <mat-datetimepicker
            #arrival
            type="datetime"
            openOnFocus="false"
            timeInterval="1"
          ></mat-datetimepicker>
</mat-form-field>

Whatever the initial value of form.get('arrival.date'), the picker don’t let me pick the 18 hour value (but it looks like clickable). When I click on it, nothing happens.

image

And if initial value is 2021-04-12T18:15 for example, the picker is preselected on 18 hour, it’s okay, but the value 17 is disabled and not clickable.

image

If my range min/max is

// example.component.ts
this.minDate = new Date('2021-04-12T17:00'); // <- there is the difference between first example
this.maxDate = new Date('2021-04-12T18:15');

Everything works well.

And if I put this range :

// example.component.ts
this.minDate = new Date('2021-04-12T17:20'); // <- there is the difference between first example
this.maxDate = new Date('2021-04-12T18:15');

The value 17 is disabled, but if you click on it, when can continue on the minutes selection, but all values are disabled …

Some idea ?

Versions of things:

"@angular/animations": "^10.1.0",
"@angular/cdk": "^11.0.2",
"@angular/common": "^10.1.0",
"@angular/compiler": "^10.1.0",
"@angular/core": "^10.1.0",
"@angular/flex-layout": "^11.0.0-beta.33",
"@angular/forms": "^10.1.0",
"@angular/material": "^11.0.2",
"@angular/platform-browser": "^10.1.0",
"@angular/platform-browser-dynamic": "^10.1.0",
"@angular/router": "^10.1.0",
"@mat-datetimepicker/core": "^5.1.2",

PS : I use two custom adapter :

// module.ts
 providers: [
    { provide: DateAdapter, useClass: CustomDateAdapter },
    { provide: DatetimeAdapter, useClass: CustomDatetimeAdapter }
],
// custom-date-adapter.ts
import { NativeDateAdapter } from '@angular/material/core';

/** Adapts the native JS Date for use with cdk-based components that work with dates. */
export class CustomDateAdapter extends NativeDateAdapter {
  parse(value: any): Date | null {
    try {
      if (typeof value === 'string' && value.indexOf('.') > -1) {
        const parts = value.split(' ');

        const datePart = parts[0];
        const timePart = parts[1];

        // parse date part
        const str = datePart.split('.');

        const year = Number(str[2]);
        const month = Number(str[1]) - 1;
        const date = Number(str[0]);

        // parse time part
        let hour = 0,
          minute = 0,
          second = 0;
        if (timePart) {
          const time = timePart.split(':');
          hour = Number(time[0]);
          if (time[1]) {
            minute = Number(time[1]);
          }
          if (time[2]) {
            second = Number(time[2]);
          }
        }

        return new Date(year, month, date, hour, minute, second);
      }
      const timestamp = typeof value === 'number' ? value : Date.parse(value);
      return isNaN(timestamp) ? null : new Date(timestamp);
    } catch (err) {
      return null;
    }
  }

  // retirar quando for feito o merge da data por mmalerba
  format(date: Date, displayFormat: Object): string {
    date = new Date(
      Date.UTC(
        date.getFullYear(),
        date.getMonth(),
        date.getDate(),
        date.getHours(),
        date.getMinutes(),
        date.getSeconds(),
        date.getMilliseconds()
      )
    );
    displayFormat = Object.assign({}, displayFormat, { timeZone: 'utc' });

    const dtf = new Intl.DateTimeFormat(this.locale, displayFormat);
    return dtf.format(date).replace(/[\u200e\u200f]/g, '');
  }
}
//custom-datetime-adapter.ts
import { NativeDatetimeAdapter } from '@mat-datetimepicker/core';

/** Adapts the native JS Date for use with cdk-based components that work with dates. */
export class CustomDatetimeAdapter extends NativeDatetimeAdapter {

  // retirar quando for feito o merge da data por mmalerba
  format(date: Date, displayFormat: Object): string {
    date = new Date(
      Date.UTC(
        date.getFullYear(),
        date.getMonth(),
        date.getDate(),
        date.getHours(),
        date.getMinutes(),
        date.getSeconds(),
        date.getMilliseconds()
      )
    );

    displayFormat = Object.assign({}, displayFormat, { timeZone: 'utc' });
    const dtf = new Intl.DateTimeFormat(this.locale, displayFormat);

    return dtf.format(date).replace(/[\u200e\u200f]/g, '').replace(',', '');
  }
}

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
neogenzcommented, May 31, 2021

Thanks ! 👍

0reactions
bborttcommented, May 27, 2021

has been merged into master in 2120778 and released in 6.0.3. thank you very much @TorstenL!

Read more comments on GitHub >

github_iconTop Results From Across the Web

MIN and MAX operators not working in Raster Calculator(s)
It seems that MIN and MAX operators are not working in the native raster calculator of QGIS and neither in the Processing raster...
Read more >
c# - WPF window buttons (close, max, min, restore) binding to ...
Seems like there's some sort of data binding problem here which is strange since I obviously don't need to bind these to data....
Read more >
A guide to the min(), max(), and clamp() CSS functions
The min(), max(), and clamp() CSS functions can revolutionize web layouts, but they can also make CSS much more difficult to reason about....
Read more >
min(), max(), and clamp(): three logical CSS functions to use ...
This essentially sets a "max" size by using the min() function. Using the clamp() function to limit a minimum and maximum width. By...
Read more >
You want minmax(10px, 1fr) not 1fr - CSS-Tricks
In the former, the minimum width of the grid column is min-content , which can be awkwardly wider than you want it to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found