i'm looping an item
object values, some of the values are dates coming from mongoDB, which look like something like this 2022-09-23T11:43:47.213Z
, with momentJS
i want to check if val
is date, then format it, otherwise display it as is, how to do that ?
<tr>
{Object.values(item).map((val, i) => (
<td key={i}>
{moment(val).isValid ? moment(val).format : val} // DOESN'T WORK
</td>
))}
</tr>
CodePudding user response:
I think format
is a function and it should be called with a format.
<tr>
{Object.values(item).map((val, i) => (
<td key={i}>
{moment(val).isValid() ? moment(val).format("MMMM Do YYYY, h:mm:ss a") : val}
</td>
))}
</tr>
Note: Same goes with isValid
. It's a function too.