2
// date October 10 2018 pmt 10:01
date = moment(details.date).format("MMMM DD YYYY at h:mm");

How to escape moment reserved keywords like a? I want the output to be October 10 2018 at 10:01.

Henok Tesfaye
  • 8,287
  • 13
  • 47
  • 84

2 Answers2

6

You can also achieve it using:

const date = moment(details.date);
const formattedDate = moment(date).format("DD MMM YYYY [at] hh:mm A");
rdj7
  • 1,905
  • 4
  • 18
  • 33
Saransh Kumar
  • 421
  • 4
  • 6
  • This is the more accurate answer. It's documented in their official docs too! Thanks you @Saransh – abdoo_salem Feb 05 '20 at 16:46
  • This should be the accepted answer. It is stated the same way in the official docs as @abdoo_salem pointed out. It is right on their landing: https://momentjs.com/ at "Format Dates". – tafaust Feb 27 '20 at 09:37
  • I faced a weird behaviour. When my escaped text has the character "m", the calendar does not show. I've tried [From: ], [Fro: ], [start: ], [smell], [me] to test and it seems when there's "m", this behaviour is observed. Any clues? – Terence Mar 03 '20 at 06:27
3

Why not just break it up into two parts?

const date = moment(details.date);
const formattedDate = `${date.format("MMMM DD YYYY")} at ${date.format("h:mm")}`;
Matthew Herbst
  • 29,477
  • 23
  • 85
  • 128