如何在 date-fns 的格式函数中添加单引号字符?

How can I add single quote character in format function of date-fns?

我想在年份之前插入一个单引号字符。 format(parseISO(date), "dd MMM '\''yy"); 这似乎不起作用。根据 date-fns 文档,要添加字符,我们需要将它们包含在两个单引号之间。我得到上面代码的输出 06 Apr 'yy 而预期输出是 06 Apr '20.

来自documentation

Two single quotes in a row, whether inside or outside a quoted sequence, represent a 'real' single quote. (see the last example)

最后一个例子展示了如何做:

// Escape string by single quote characters:
var result = format(new Date(2014, 6, 2, 15), "h 'o''clock'")
//=> "3 o'clock"

所以在你的情况下应该是:

format(parseISO(date), "dd MMM ''yy");