Skip to content

Instantly share code, notes, and snippets.

@jeroenheijmans
Last active June 5, 2018 18:19
Show Gist options
  • Save jeroenheijmans/7a4912954a9d52f45056403886fcd63b to your computer and use it in GitHub Desktop.
Save jeroenheijmans/7a4912954a9d52f45056403886fcd63b to your computer and use it in GitHub Desktop.
Parser/formatter for ngbDatepicker with dd-MM-yyyy format
// Formatter using "dd-MM-yyyy" string format:
// See: https://ng-bootstrap.github.io/#/components/datepicker/api#NgbDateParserFormatter
//
export class NgbDateStringParserFormatter extends NgbDateParserFormatter {
parse(value: string): NgbDateStruct {
if (!value) { return null; }
const parts = value.trim().split('-');
return {
day: parts.length > 0 ? parseInt(parts[0], 10) : null,
month: parts.length > 1 ? parseInt(parts[1], 10) : null,
year: parts.length > 2 ? parseInt(parts[2], 10) : null,
};
}
format(date: NgbDateStruct): string {
const pad = (n) => Number.isInteger(n) ? ('0' + n).substr(-2) : '';
return date ? `${pad(date.day)}-${pad(date.month)}-${date.year}` : '';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment