简体   繁体   English

使用自定义格式的时刻格式化日期

[英]format date using moment with custom format

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 我有一个自定义格式,并使用它与moment并得到了这个

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? 请注意,模式中的占位符de实际上已被解析..有没有办法让我以预期的格式获取日期segunda-feira, 2 de janeiro de 2017使用时刻?

You have to use [] square brackets to escape characters in format string, see format docs: 您必须使用[]方括号来转义格式字符串中的字符,请参阅format文档:

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 . 此外请注意,目前没有EEEE令牌,但单个E代表星期几(ISO) ,因此在您的情况下,您将拥有2222 Use dddd to get the desidered output. 使用dddd获取所需的输出。

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> 

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> 

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

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