I'm facing an issue with converting the date format from ngbDatepicker
to a string before sending the data to my backend API. The API only accepts dates in string format, so I attempted to convert it using
submittedData.MaturityDate.toString();
and submittedData.RemitDate.toString();
, but it's not working as expected.
In my component.html
, I have the following form:
<div class="form-group row">
<div class="col-sm-6">
<label for="MaturityDate">Maturity Date</label>
...
</div>
<div class="col-sm-6">
<label for="RemitDate">Remittance Date</label>
...
</div>
</div>
</div>
When the form is submitted, the following function is triggered in my component.ts
:
onSubmit() {
this.submitted = true;
// Validate form
if (this.accountPayableForm.invalid) {
return;
}
let submittedData = this.accountPayableState;
submittedData = this.accountPayableForm.value;
// **CONVERSION TO STRING
submittedData.MaturityDate.toString();
submittedData.RemitDate.toString();
console.log(submittedData);
this.loading = true;
...
);
}
This is the submitAccountPayable()
function in my service.ts
:
submitAccountPayable(accountPayable: AccountPayableState) {
return this.http.post(this.issueAccountPayableUrl, accountPayable);
}
The attributes MaturityDate
and RemitDate
in my AccountPayableState
model are meant to be strings. However, even after using .toString()
, they are not being converted correctly. Here's a snippet of the payload in the console:
BuyerAccount: "asdasdasd"
BuyerCode: "123123"
MaturityDate: {year: 2019, month: 10, day: 18}
OwningAccountKey: "e273b89f-c828-41ad-873d-a13bbe63d7c5"
...
RemitDate: {year: 2019, month: 10, day: 10}
SupplierAccount: "asdasdasda"
SupplierCode: "123123123"
Any advice on how to resolve this issue?