为什么我的 json 日期字符串对 date-fns 无效?

Why is my json date string Invalid for date-fns?

我知道以前有人问过这个问题。但是查看这些答案我无法弄清楚。我有一个 Json 日期字符串 docdate: "2020-10-02T00:00:00",我正在尝试将其转换为日期以供显示。我使用 System.Text.Json 在 API 中序列化它。我正在使用 date-fns 节点模块:

import { format } from 'date-fns';
...
<td>{format(order.docdate, 'yyyy/MM/dd')}</td>

... 给我这个错误:

RangeError: Invalid time value
370 | var originalDate = toDate(dirtyDate);
371 | 
372 | if (!isValid(originalDate)) {
373 |   throw new RangeError('Invalid time value');
374 | } // Convert the date in system timezone to the same date in UTC+00:00 timezone.
375 | // This ensures that when UTC functions will be implemented, locales will be compatible with them.
376 | // See an issue about UTC functions: https://github.com/date-fns/date-fns/issues/376

为什么我的日期字符串无效?

format from date-fns 接受日期对象或数字,而不是字符串。

您应该首先使用 date-fns 中的 parseparseISO 解析日期字符串(给出一个 Date 对象),或者内置的 Date.parse() 给出一个可以传递给 format.

的数字