How can I use date-fns to format a date from an API?

91 views Asked by At

I receive data from an API in the format 'yyyy-MM-dd'. I would like to convert this date to 'dd-MM-yyyy'. When I specify this notation my table will not be displayed. How could I change the format in a proper way?

 onMounted(async () => {
    const today = startOfDay(new Date());
    const lastWeek = sub(today, { days: 7 });
    const searchParams: TableSearchParams = {
      startDate: format(lastWeek, 'yyyy-MM-dd'),
    };
    const { data } = await fetchData(searchParams);
    if (data && data.entries) {
      tableRows.value = data.entries.map((entry) => ({
        ...entry,
        lastDelivery: entry.values ? entry.values[entry.values.length - 1].date : '-',
      }));
    } else {
      tableRows.value = [];
    }
  });
1

There are 1 answers

0
Saturday Sun On

The new date format is not accepted by the server. If the server does not recognize the new date format, no data will be returned. In this case, you may need to change the date format to one that is accepted by the server or configure the server accordingly.