Skip to content

Instantly share code, notes, and snippets.

@tomahim
Created January 10, 2018 10:20
Show Gist options
  • Save tomahim/a1d6fa0bdb30f27ad56b01ce019f2118 to your computer and use it in GitHub Desktop.
Save tomahim/a1d6fa0bdb30f27ad56b01ce019f2118 to your computer and use it in GitHub Desktop.
NgbDate momentJS adapter for Bootstrap datepicker
import { Injectable } from '@angular/core';
import { NgbDateAdapter, NgbDateStruct } from '@ng-bootstrap/ng-bootstrap';
import * as moment from 'moment';
@Injectable()
export class NgbDateMomentAdapter extends NgbDateAdapter<moment.Moment> {
fromModel(date: moment.Moment): NgbDateStruct {
if (!date) {
return null;
}
return { year: date.year(), month: date.month(), day: date.day() };
}
toModel(date: NgbDateStruct): moment.Moment {
if (!date) {
return null;
}
return moment(date.year + '-' + date.month + '-' + date.day, 'YYYY-MM-DD');
}
}
@jeroenheijmans
Copy link

jeroenheijmans commented Jun 5, 2018

This was a helpful reference implementation, but I believe there might be two bugs with it:

For what it's worth to someone, I've created this public gist with a different implementation that doesn't seem to have those issues (and some jasmine tests to check that it doesn't).

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