0

In date picker onchange method I have passed the date as string like ddmmyyyy (20102020) to dd/mm/yyyy. I don't know how to convert the date format to dd/mm/yyyy.In customerDateParserFormatter string needs to be convertfrom ddmmyyyy to dd/mm/yyyyy

@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) {}

In this method I get the value from date picker click as {year:2020,month:10,day:10}. At the same time, the user enters the date instead of click in input field I receive the value as a string. I don't know how to convert format and don't know to proceed. Kindly understand my problem.

Sathish
  • 149
  • 2
  • 19
  • I have anwered this question here. Check 2nd answer in [this](https://stackoverflow.com/questions/56685403/how-to-change-ngbdatepicker-date-format-from-json-to-yyyy-mm-dd/61234446#61234446) link – Venkatesh K Apr 27 '20 at 15:26

1 Answers1

0

I have answered this question here. Check 3rd answer in this link. I've used yyyy-mm-dd, you can use dd-mm-yyyy format in the typescript code. Just modify the last line to
let finalDate = day + "/"+ month + "/" + year ;

Venkatesh K
  • 133
  • 1
  • 1
  • 11