简体   繁体   English

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

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

I want to insert a single quote character before year.我想在年份之前插入一个单引号字符。 format(parseISO(date), "dd MMM '\\''yy"); this does not seem to work.这似乎不起作用。 According to date-fns documentation, to add characters, we need to include them between two single quotes.根据 date-fns 文档,要添加字符,我们需要将它们包含在两个单引号之间。 I get the output for the above code as 06 Apr 'yy whereas the expected output is 06 Apr '20 .我得到上述代码的输出为06 Apr 'yy而预期的输出为06 Apr '20

From the documentation :文档

Two single quotes in a row, whether inside or outside a quoted sequence, represent a 'real' single quote.一行中的两个单引号,无论是在带引号的序列内部还是外部,都表示“真正的”单引号。 (see the last example) (见最后一个例子)

The last example shows how to do it:最后一个例子展示了如何做到这一点:

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

So in your case it should be:所以在你的情况下它应该是:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM