最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Angular 4 datepicker locale not working properly - Stack Overflow

programmeradmin0浏览0评论

I'm using ng-pick-datetime UI ponent for the date picker. I disabled readonly mode, so that the input is active for typing with keyboard.

<owl-date-time name="client_birthday" #client_birthday="ngModel"
    [placeHolder]="'DD.MM.YYYY'"
    [locale]="ru"
    [(ngModel)]="client.client_birthday"
    [type]="'calendar'"
    [dataType]="'string'"
    [dateFormat]="'DD.MM.YYYY'"
    [readonlyInput] ="false"
    [autoClose] ="true"
    [inputId]="'client_birthday'"                                                   
></owl-date-time>

I try to apply RU locale using date-fns:

ru: any;
ruLocale = require('date-fns/locale/ru');
ngOnInit() {     
    this.ru = {
        firstDayOfWeek: 1,
        dayNames: [...],
        dayNamesShort: [...],
        monthNames: [...],
        monthNamesShort: [...],
        dateFns: this.ruLocale
    };
}

When I type into the input with dd.mm.yyyy format, the picker makes day as a month and month as a day, and the year is ok. So, the only format it recognizes is mm.dd.yyyy, which seems to be us locale. My calendar language changed as expected, but is it possible to configure date format for RU as well?

I would appreciate any help.

I'm using ng-pick-datetime UI ponent for the date picker. I disabled readonly mode, so that the input is active for typing with keyboard.

<owl-date-time name="client_birthday" #client_birthday="ngModel"
    [placeHolder]="'DD.MM.YYYY'"
    [locale]="ru"
    [(ngModel)]="client.client_birthday"
    [type]="'calendar'"
    [dataType]="'string'"
    [dateFormat]="'DD.MM.YYYY'"
    [readonlyInput] ="false"
    [autoClose] ="true"
    [inputId]="'client_birthday'"                                                   
></owl-date-time>

I try to apply RU locale using date-fns:

ru: any;
ruLocale = require('date-fns/locale/ru');
ngOnInit() {     
    this.ru = {
        firstDayOfWeek: 1,
        dayNames: [...],
        dayNamesShort: [...],
        monthNames: [...],
        monthNamesShort: [...],
        dateFns: this.ruLocale
    };
}

When I type into the input with dd.mm.yyyy format, the picker makes day as a month and month as a day, and the year is ok. So, the only format it recognizes is mm.dd.yyyy, which seems to be us locale. My calendar language changed as expected, but is it possible to configure date format for RU as well?

I would appreciate any help.

Share Improve this question asked Nov 20, 2017 at 6:14 GyuzalGyuzal 1,59111 gold badges54 silver badges105 bronze badges 4
  • Have u installed date-fns too? – Rajkumar Rathi Commented Nov 22, 2017 at 12:16
  • @RajkumarRathi, yes – Gyuzal Commented Nov 22, 2017 at 12:32
  • github./DanielYKPan/date-time-picker#localization does this solve problem – Naresh Thakur Commented Nov 22, 2017 at 21:12
  • @thakurinbox, this is the same I did – Gyuzal Commented Nov 23, 2017 at 3:31
Add a ment  | 

2 Answers 2

Reset to default 6 +25

It seams unable to do what you want when input date by typing on input box of ng-pick-datetime. I went through the implementation of the ng-pick-datetime Component. When typing, given input converting to date object by using https://date-fns/v1.29.0/docs/parse Here is the implementation picker.ponent.ts/src/picker.ponent.ts Line-1177 Method-parseToDate

/**
     * Parse a object to Date object
     * @param {any} val
     * @return {Date}
     * */
    private parseToDate( val: any ): Date {
        if (!val) {
            return;
        }

        let parsedVal;
        if (typeof val === 'string') {
            parsedVal = parse(val, this.dateFormat, this.now);
        } else {
            parsedVal = val;
        }

        return isValid(parsedVal) ? parsedVal : null;
    }

https://github./DanielYKPan/date-time-picker has used "date-fns": "^2.0.0-alpha.7g" on package json. It has a argument to pass the date format to parse date.

parsedVal = parse(val, this.dateFormat, this.now);

But latest version of date-fns: "^1.29.0", Do not have parmeter to pass the date format. So it fails on your project too. Please read https://date-fns/v1.29.0/docs/parse

parse(argument, [options])

similar to this:

@NgModule({
  providers: [
    { provide: LOCALE_ID, useValue: "ru-ru"},
    //otherProviders...
  ]
})

have a look at this How to set locale in DatePipe in Angular 2?

发布评论

评论列表(0)

  1. 暂无评论