When I use the date picker onchange method, the date is passed as a string like ddmmyyyy
(20102020) to dd/mm/yyyy
. I am unsure of how to convert the date format from ddmmyyyy
to dd/mm/yyyy
. In the CustomDateParserFormatter, the string needs to be converted from ddmmyyyy to dd/mm/yyyy.
@Injectable()
export class CustomDateParserFormatter {
parse(value: string): NgbDateStruct {
if (!value) {
return null;
}
const parts = value;
parts.split('/');
console.log("first calling");
return { year: +parts[0], month: +parts[1], day: +parts[2] } as NgbDateStruct;
}
format(date: NgbDateStruct): string {
// tslint:disable-next-line:prefer-template
console.log("second calling");
return date ? ('0' + date.day).slice(-2) + '/' + ('0' + date.month).slice(-2) + '/' + date.year : null;
}
}
<input class="form-control"
placeholder="DD/MM/YYYY"
ngbDatepicker
formControlName="dateofBrith"
[minDate]="{year: 1975, month: 1, day: 1}"
[maxDate]="{year: 2020, month: 12, day: 31}"
name="dp"
#d="ngbDatepicker"
(ngModelChange)="changeDob($event)"
maxlength="10">
<div class="input-group-append">
<button class="btn calendar btn-custom btn-outline-secondary"
(click)="d.toggle()"
type="button"><img src="/assets/images/dateicon.png"></button>
</div>
changeDob(value) {}
When using this method, I receive the value from the date picker onclick as {year:2020,month:10,day:10}
. However, when the user manually enters the date in the input field instead of clicking, the value is received as a string. I am struggling with converting the format and need guidance on how to proceed. Your understanding is greatly appreciated.