Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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