【发布时间】:2015-09-18 13:24:10
【问题描述】:
我遵循严格的用户界面指南,我需要在上标 (<sup>) 中显示日期后缀:
18th 2015 年 9 月
MomentJS 有很多格式化日期的功能,但不幸的是,它似乎不允许我们在不包括前面的数字的情况下提取日期后缀(上例中的 th 位)...
Token Output Month Mo 1st 2nd ... 11th 12th Day of Month Do 1st 2nd ... 30th 31st Day of Year DDDo 1st 2nd ... 364th 365th ...
目前我正在使用以下方法去除后缀之前的数值:
date.format("Do").replace(/\d/g, "");
--> "18th" -> "th"
但问题是,当必须显示“2015 年 9 月 18 日”之类的内容时,这会变得混乱,因为我必须使用三个单独的 format() 调用:
var dateSuffix = "<sup>" + date.format("Do").replace(/\d/g, "") + "</sup">;
date.format("DD") + dateSuffix + date.format("MMMM YYYY");
--> "18<sup>th</sup> September 2015"
理想情况下,我希望完全避免使用replace(),而是使用与此类似的东西,但将Do 部分替换为仅后缀:
moment(new Date()).format("DD<\\sup>Do</\\sup> MMMM YYYY");
--> "18<sup>18th</sup> September 2015"
MomentJS 是否有任何功能可以自行提取日期后缀?
【问题讨论】:
标签: javascript momentjs