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

1.9k views Asked by At

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. I get the output for the above code as 06 Apr 'yy whereas the expected output is 06 Apr '20.

1

There are 1 answers

0
Barmar On BEST ANSWER

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");