I'm using ng-pick-datetime UI component 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.
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.org/v1.29.0/docs/parse Here is the implementation picker.component.ts/src/picker.component.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.com/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.org/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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With