2
var format = 'EEEE, D 'de' MMMM 'de' Y'
moment(date).format(format);

I have a custom format and use it with moment and got this

exp: segunda-feira, 2 de janeiro de 2017
act: Segunda-feira, 2 11 Janeiro 11 2017

Note the placeholder de in the pattern actually got parsed .. Is there a way for me to get the date in the expected format segunda-feira, 2 de janeiro de 2017 using moment?

codereviewanskquestions
  • 13,460
  • 29
  • 98
  • 167
  • Possible duplicate of [Parse 'Date & Time' string in Javascript which are of custom format](http://stackoverflow.com/questions/28002261/parse-date-time-string-in-javascript-which-are-of-custom-format) – matt Mar 07 '17 at 01:49

2 Answers2

7

You have to use [] square brackets to escape characters in format string, see format docs:

To escape characters in format strings, you can wrap the characters in square brackets.

Moreover note that there is no EEEE token in moment, but the single E represents Day of Week (ISO), so in your case you will have 2222. Use dddd to get the desidered output.

Here a working example:

var date = '2017-01-02';
var format = 'dddd, D [de] MMMM [de] YYYY';
console.log(moment(date).format(format));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.1/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.1/locale/pt.js"></script>
VincenzoC
  • 30,117
  • 12
  • 90
  • 112
0

To escape characters in format strings, you can wrap the characters in square brackets.

var momObj = moment();
var format = 'EEEE, D [de] MMMM [de] Y';
var fString = momObj.format(format);
console.log(fString);
<script src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.13.0/moment.min.js"></script>
Satpal
  • 132,252
  • 13
  • 159
  • 168