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
How to use moment.js dates for the directives? #3218
Comments
Bad formatting in the adapter causes the null exceptions but now fixed with the below implementation. import { Injectable } from "@angular/core";
import { NgbDateAdapter, NgbDateStruct } from "@ng-bootstrap/ng-bootstrap";
import * as moment from "moment";
import * as _ from "lodash";
@Injectable()
export class MomentNgbDateAdapterService extends NgbDateAdapter<moment.Moment> {
constructor() {
super();
}
fromModel(value: moment.Moment): NgbDateStruct {
return value && moment.isMoment(value)
? {
day: value.date(),
month: value.month() + 1,
year: value.year()
}
: null;
}
toModel(date: NgbDateStruct): moment.Moment {
return date &&
_.isInteger(date.year) &&
_.isInteger(date.month) &&
_.isInteger(date.day)
? moment({
year: date.year,
month: date.month - 1,
date: date.day,
hour: 12
})
: null;
}
} But still, I cannot use MomentJS for directives. |
I wouldn't use this approach. First of all you are importing the entire Lodash library in order to use one method (isInteger), you can cherry-pick methods in Lodash or, even better, use native Moment it's great but it's not tree-shakeable and besides that when bundling with Angular CLI it contains locales wich is very annoying. I suggest using Luxon or, as i do, use date-fns v2 wich is fully tree shakeable and covers almost the entirety Moment's API surface. |
@meness What's going on with the directives ? You told that the DateAdpater is now working, fine, but in a directive, you can just use the moment objects, right ? An example of what you want to achieve would be welcome ! |
Good point! We would really appreciate a reproducible scenario inside a Stackblitz to simply understand what you want to achieve. |
Hi,
I expect to use different date object for the directives while defined a custom provider. Sometimes, when I select a date from future the input receives an empty string/null. Moreover, the dates defined for directives do not work and default (+10-10 years) displays. By the way,
NgbDateStruct
date objects work well for directives but I expect the custom date object to work properly.The text was updated successfully, but these errors were encountered: