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 German Format #5041

Closed
weisss123 opened this issue Jun 9, 2017 · 10 comments
Closed

Datepicker German Format #5041

weisss123 opened this issue Jun 9, 2017 · 10 comments

Comments

@weisss123
Copy link

weisss123 commented Jun 9, 2017

Bug, feature request, or proposal:

Bug

What is the expected behavior?

If I choose a date in format dd.mm.yyyy (Datepicker Module) I expect that the popup will appear in month mm.

What is the current behavior?

Problem in Datepicker Module:
If dd<=12 then the popup appears in month dd instead of mm.

What are the steps to reproduce?

Just choose a date with dd<=12
I am even able to reproduce it on your site: https://material.angular.io/components/component/datepicker

What is the use-case or motivation for changing an existing behavior?

Which versions of Angular, Material, OS, TypeScript, browsers are affected?

Is there anything else we should know?

@donroyco
Copy link
Contributor

donroyco commented Jun 9, 2017

See #4358

@julianobrasil
Copy link
Contributor

julianobrasil commented Jun 9, 2017

Also take a look at this plunker (with a dateadapter), it's working for brazilian portuguese: https://plnkr.co/edit/FlgGpjqyDlypas0VZJzo?p=preview

@JunusErgin
Copy link

JunusErgin commented Jun 12, 2017

Hello weisss123,

add the following lines to your AppComponent to get German formatting.

constructor(...,
              private dateAdapter: DateAdapter<Date>,
               ...) {
    this.dateAdapter.setLocale('de');

Add a provider to your module:

providers: [{provide: DateAdapter, useClass: GermanDateAdapter}],

and create a new file GermanDateAdapter:

import { NativeDateAdapter } from '@angular/material';

export class GermanDateAdapter extends NativeDateAdapter {
  parse(value: any): Date | null {
    if ((typeof value === 'string') && (value.indexOf('.') > -1)) {
      const str = value.split('.');
      if (str.length < 2 || isNaN(+str[0]) || isNaN(+str[1]) || isNaN(+str[2])) {
        return null;
      }
      return new Date(Number(str[2]), Number(str[1]) - 1, Number(str[0]), 12);
    }
    const timestamp = typeof value === 'number' ? value : Date.parse(value);
    return isNaN(timestamp) ? null : new Date(timestamp);
  }
}

Hope that helps

Junus

@weisss123
Copy link
Author

Thank you guys. This worked for me

@JunusErgin
Copy link

This is not documented very good in https://material.angular.io/components/datepicker/overview
Is there a way to add my example to this page or some Wiki?

@donroyco
Copy link
Contributor

@JunusErgin See #6030 and #5722

@zszep
Copy link

zszep commented Oct 5, 2017

@JunusErgin The parsing is incomplete. I.e. you can enter dates like 33.45.2017 and the control is not marked as invalid. I think the parsing should be much more complex to handle German (and in my case Croatian which is exactly the same as German) date parsing.

@aboglioli
Copy link

You can use moment.js to validate the date

@stefantrost
Copy link

stefantrost commented Mar 18, 2018

@JunusErgin @zszep The DateAdapter class has the method createDate(), that has built-in validation. So instead of returning a new Date(Number(str[2]), Number(str[1]) - 1, Number(str[0]), 12); I'm returning the result of super.createDate(Number(str[2]), Number(str[1]) - 1, Number(str[0])); and it works fine for me.

@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Sep 8, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants