使用MomentJS提取日期后缀以使其成为上标 [英] Extract a date suffix with MomentJS to make it superscript

查看:114
本文介绍了使用MomentJS提取日期后缀以使其成为上标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遵循严格的用户界面指南,我需要在上标中显示日期后缀(< sup> ):

I'm following strict user interface guidelines and I need to display date suffixes in superscript (<sup>):


18 th 2015年9月


18th September 2015

MomentJS有很多用于格式化日期的函数,但不幸的是它似乎不允许我们提取日期后缀(上面示例中的 th 位)而不包括它之前的数字。 ..

MomentJS has a lot of functions for formatting dates, but unfortunately it doesn't seem to allow us to extract a date suffix (the th bit in the example above) without including the number before it...


                Token    Output
Month           Mo       1st 2nd ... 11th 12th
Day of Month    Do       1st 2nd ... 30th 31st
Day of Year     DDDo     1st 2nd ... 364th 365th
...


目前我正在使用以下方法剥离后缀之前的数值:

Currently I'm stripping the numeric values before the suffix by using:

date.format("Do").replace(/\d/g, "");
--> "18th" -> "th"

但问题是,当显示诸如2015年9月18日之类的内容时,这会变得混乱,因为我不得不使用三个单独的格式()来电:

But the problem is that this gets messy when having to display things like "18th September 2015", as I'm having to use three separate format() calls:

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 部分仅替换后缀:

I'm ideally looking to avoid using replace() altogether and to instead use something similar to this but with the Do part replaced with just the suffix:

moment(new Date()).format("DD<\\sup>Do</\\sup> MMMM YYYY");
--> "18<sup>18th</sup> September 2015"

MomentJS是否有任何功能可以拉动日期后缀本身?

Does MomentJS have any functionality to pull a date suffix by itself?

推荐答案

我不认为MomentJS具有您正在寻找的功能;我想你应该去MomentJS网站并将其作为功能建议提交。你永远不会知道,甚至可能会实现。

I don't think MomentJS has the function you're looking for; I'd say you should go to the MomentJS site and submit it as a feature suggestion. You never know, it might even get implemented.

同时,另一种选择可能是使用MomentJS中的配置选项来更改Ordinal字符串(即后缀) )它们包含< sup> 标记。

In the meanwhile, an alternative option might be to use the configuration options in MomentJS to change the Ordinal strings (ie the suffixes) so that they include a <sup> tag.

请参阅MomentJS手册中的相关部分: http://momentjs.com/docs/#/customization/ordinal/

See the relevant section in the MomentJS manual here: http://momentjs.com/docs/#/customization/ordinal/

你最终会编写一个如下所示的函数:

You'd end up writing a function that looks like this:

moment.locale('en', {
    ordinal : function (number, token) {
        var b = number % 10;
        var output = (~~ (number % 100 / 10) === 1) ? 'th' :
            (b === 1) ? 'st' :
            (b === 2) ? 'nd' :
            (b === 3) ? 'rd' : 'th';
        return number + '<sup>' + output + '</sup>';
    }
});

以上示例直接取自MomentJS的手册,只需返回正在修改行。

The above example is lifted directly from MomentJS's manual, with just the return line being modified.

这篇关于使用MomentJS提取日期后缀以使其成为上标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆