I am using React, and receiving the following data in Typescript. I want to display date in this format. mm/dd/yyyy,
export type Payment = {
paymentId: number;
paymentDate: Date;
}
Data looks like this example: 2022-02-11T20:00:00.000Z
I cannot do this, as it gives error: date .toISOString is not a function
{payment.paymentDate.toISOString().substring(0, 10)}
I have to reconvert into Date again to show. All my dates are valid in checking data, so why do I need to reconvert again?
{new Date(payment.paymentDate).toISOString().substring(0, 10)}
CodePudding user response:
Seems like your paymentDate
isn't really a JavaScript Date
, but a `string.
string
doesn't have toIsoString
method
TypeScript is only a build time tool, it can't enforce data type.
I would use a library like date-fns for example, but other than that, you can use Where can I find documentation on formatting a date in JavaScript?