Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DatePicker doesn't recognize manual date input when using nzFormat #3976

Closed
mironante opened this issue Aug 13, 2019 · 9 comments · Fixed by #4833
Closed

DatePicker doesn't recognize manual date input when using nzFormat #3976

mironante opened this issue Aug 13, 2019 · 9 comments · Fixed by #4833
Assignees

Comments

@mironante
Copy link

Reproduction link

https://stackblitz.com/edit/angular-a8d35n

Steps to reproduce

Add DatePicker component and set nzFormat="dd.MM.yyyy".

<nz-date-picker [(ngModel)]="date" nzFormat="dd.MM.yyyy"></nz-date-picker>

Manual input works only if date and month are same. For example, if we type 04.04.1995 it works, but 04.05.1995 is not working.

What is expected?

After writing string date, DatePicker changes date in the model.

What is actually happening?

Nothing happens

Environment Info
ng-zorro-antd 8.1.2
Browser Chrome 76

Default format working fine

@wenqi73
Copy link
Member

wenqi73 commented Aug 13, 2019

Thanks for your report, we will support it until dateFns release 2.0.0. (#2492 also reported it)

@darshanrampatel
Copy link
Contributor

@wenqi73 date-fns has now released 2.0.0 so can this be looked at again please?

@wenqi73
Copy link
Member

wenqi73 commented Aug 21, 2019

@darshanrampatel Sure, we will work it out soon.

@soulasvalentin
Copy link

Hello! Are there any updates on this? Thanks!

@valerio-lucantonio
Copy link

Guys any update?

@binle
Copy link

binle commented Feb 12, 2020

I think problem is here:
components/date-picker/lib/calendar/calendar-input.component.ts

 onInputKeyup(event: KeyboardEvent, isEnter: boolean = false): void {
    const date = this.checkValidInputDate(event);

    if (!date || (this.disabledDate && this.disabledDate(date.nativeDate))) {
      return;
    }

    this.value = date;
    this.valueChange.emit({ date, isEnter });
  }
  private checkValidInputDate(event: Event): CandyDate | null {
    const input = (event.target as HTMLInputElement).value;
    const date = new CandyDate(input);

    this.invalidInputClass = '';
    if (!date.isValid() || input !== this.toReadableInput(date)) {
      // Should also match the input format exactly
      this.invalidInputClass = `${this.prefixCls}-input-invalid`;
      return null;
    }

    return date;
  }

Problem is const date = new CandyDate(input);. You should support new CandyDate with dev's format or convert to date with dev's format before new CandyDate.
input now is string, and in CadyDate(components/core/time/candy-date.ts) it will create nativeDate = new Date(date);
nativeDate will be invalid with dev's format(EX dd/MM/yyyy)
Please take a look, thanks!

vthinkxie pushed a commit that referenced this issue Mar 15, 2020
* feat(module:date-picker): support parse input value

* test: fix tests

* chore: support datefnsCompat option

* fix: not import from i18n in core

* chore: change API name and add docs

* chore: change format convert from config to token

* docs: update docs

* chore: move some types

* docs: update docs for date-fns update

* chore: change name and add warning

* docs: update docs
Close #4028
Close #3976
Close #2492
Close #4101
Ricbet pushed a commit to Ricbet/ng-zorro-antd that referenced this issue Apr 9, 2020
* feat(module:date-picker): support parse input value

* test: fix tests

* chore: support datefnsCompat option

* fix: not import from i18n in core

* chore: change API name and add docs

* chore: change format convert from config to token

* docs: update docs

* chore: move some types

* docs: update docs for date-fns update

* chore: change name and add warning

* docs: update docs
Close NG-ZORRO#4028
Close NG-ZORRO#3976
Close NG-ZORRO#2492
Close NG-ZORRO#4101
@hsuanxyz hsuanxyz mentioned this issue Apr 15, 2020
@xdrago1
Copy link

xdrago1 commented Jul 1, 2020

it is still not working on 9.2.2
That is really sad. Is there no workaround?

@wenqi73
Copy link
Member

wenqi73 commented Jul 2, 2020

@xdrago1 You should import date-fns by providing NZ_DATE_LOCALE.
https://ng.ant.design/docs/i18n/en#how-to-format-a-date-using-date-fns

hsuanxyz pushed a commit to hsuanxyz/ng-zorro-antd that referenced this issue Aug 5, 2020
* feat(module:date-picker): support parse input value

* test: fix tests

* chore: support datefnsCompat option

* fix: not import from i18n in core

* chore: change API name and add docs

* chore: change format convert from config to token

* docs: update docs

* chore: move some types

* docs: update docs for date-fns update

* chore: change name and add warning

* docs: update docs
Close NG-ZORRO#4028
Close NG-ZORRO#3976
Close NG-ZORRO#2492
Close NG-ZORRO#4101
@codersgyan
Copy link

I managed to solve this issue using date-fns Locale.
I was looking for the Russian date format (dd.mm.yyyy). By changing prop nzFormat="dd.mm.yyyy", it didn't work.
So what I did is, In app.module.ts.

import { ru as RU} from 'date-fns/locale';

providers: [
    { provide: NZ_DATE_LOCALE, useValue: RU },
  ],

By doing this, all my date pickers worked fine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment