In my Angular application, template driven form, I have a date of birth field and added ngbDatepicker for that field as given.
<input #dob="ngbDatepicker"
(click)="dob.toggle()"
[(ngModel)]="model.dob"
[ngClass]="{ 'is-invalid': f.submitted && dob.invalid }"
class="form-control"
id="dob"
name="dob"
required
type="text"
[disabled]="isDisabled"
[class.ash]="isDisabled"
[class.highlight]="!isDisabled"
ngbDatepicker
/>
I'm getting date of birth form back-end API as dob: "2019-02-16 00:00:00"
and I want to pass that value like bellow,
{
"year": 2019,
"month": 2,
"day": 26
}
because ngbDatepicker get value in that format. This is what I tried to convert my date of birth.
toDate(dob) {
const [year, month, day] = dob.split('-');
const obj = { year: year, month: month, day: day.split(' ')[0].trim() };
console.log('convert date', obj);
this.model.dob = obj;
// this.model.dob ={year: 2017, month: 5, day: 13};
}
The output is {year: "2019", month: "02", day: "16"}
, I want to remove quotation marks from this output. I've tried so many methods and unable to get the needed output.
I could get {"year": 2019, "month": 02, "day": 16}
this output using bellow code.
JSON.stringify({ "year": "2019", "month": "2", "day": "26" }, (key, value) => !isNaN(+value) ? +value : value);
But to set the date I need to set object like this. {year: 2019, month: 2, day: 26 }
I solved my problem by doing this.
Create NgbDate instance by,
date = new NgbDate(2020,19,02);
make this date as ngModel;