我想动态生成基于当前一天的文本字符串。所以,例如,如果它是第1天,那么我想我的代码生成=“它的<动态>1*<动态字符串>st</动态字符串>*</动态>”。

总共有12天,所以我做了以下事情:

我设置了一个for循环,循环12天。 在我的html中,我已经给了我的元素一个唯一的id来瞄准它,如下所示: <h1 id="dynamicTitle" class="CustomFont leftHeading shadow">On The <span></span> <em>of rest of generic text</em></h1> 然后,在我的for循环中,我有以下代码: $ (" # dynamicTitle跨度”). html(我); Var day = i; If (day == 1) { Day = I +“st”; } else if (day == 2) { 日= I + "nd" } else if (day == 3) { Day = I + "rd" }

更新

这是请求的整个for循环:

$(document).ready(function () {
    for (i = 1; i <= 12; i++) {
        var classy = "";
        if (daysTilDate(i + 19) > 0) {
            classy = "future";
            $("#Day" + i).addClass(classy);
            $("#mainHeading").html("");
            $("#title").html("");
            $("#description").html("");
        } else if (daysTilDate(i + 19) < 0) {
            classy = "past";
            $("#Day" + i).addClass(classy);
            $("#title").html("");
            $("#description").html("");
            $("#mainHeading").html("");
            $(".cta").css('display', 'none');
            $("#Day" + i + " .prizeLink").attr("href", "" + i + ".html");
        } else {
            classy = "current";
            $("#Day" + i).addClass(classy);
            $("#title").html(headings[i - 1]);
            $("#description").html(descriptions[i - 1]);
            $(".cta").css('display', 'block');
            $("#dynamicImage").attr("src", ".." + i + ".jpg");
            $("#mainHeading").html("");
            $(".claimPrize").attr("href", "" + i + ".html");
            $("#dynamicTitle span").html(i);
            var day = i;
            if (day == 1) {
                day = i + "st";
            } else if (day == 2) {
                day = i + "nd"
            } else if (day == 3) {
                day = i + "rd"
            } else if (day) {
            }
        }
    }

当前回答

从Shopify

函数getNumberWithOrdinal(n) { Var s = ["th", "st", "nd", "rd"], V = n % 100; 返回n + (s[(v - 20) % 10] || s[v] || s[0]); } [4 1, 0, 1, 2, 3, 4, 10, 11, 12日,13日,14日,20日,21日,22100101111].forEach ( n => console.log(n + ' -> ' + getNumberWithOrdinal(n)) );

其他回答

我写了这个函数来解决这个问题:

// this is for adding the ordinal suffix, turning 1, 2 and 3 into 1st, 2nd and 3rd
Number.prototype.addSuffix=function(){
    var n=this.toString().split('.')[0];
    var lastDigits=n.substring(n.length-2);
    //add exception just for 11, 12 and 13
    if(lastDigits==='11' || lastDigits==='12' || lastDigits==='13'){
        return this+'th';
    }
    switch(n.substring(n.length-1)){
        case '1': return this+'st';
        case '2': return this+'nd';
        case '3': return this+'rd';
        default : return this+'th';
    }
};

有了这个,你可以把. addsuffix()放在任何数字上,它就会得到你想要的结果。例如:

var number=1234;
console.log(number.addSuffix());
// console will show: 1234th

Intl。PluralRules,标准方法。

我只是想在这里放弃做这个的规范方法,因为似乎没有人知道它。不要白费力气。

如果你想让你的代码

自我记录 易于本地化 用现代标准

-这才是正确的方法。

const english_ordinal_rules = new Intl。PluralRules("en", {type: "ordinal"}); Const后缀= { 一:“圣”, 二:“和”, 一些:“路”, 其他:“th” }; 函数序数(number/*: number */) { Const category = english_ordinal_rules.select(number); Const suffix =后缀[类别]; 返回(数字+后缀); } // ->字符串 const test =数组(201) .fill () .map((_, index) => index - 100) . map(顺序) . join (" "); console.log(测试);

Intl。PluralRules构造函数(ECMA-402草案) 统一码的六个多元类别


Code-golf

虽然我不建议用你的代码打高尔夫,杀死可读性,但我为那些高尔夫球手想出了一个(92字节):

n=>n+{e:"st",o:"nd",w:"rd",h:"th"}[new Intl.PluralRules("en",{type:"ordinal"}).select(n)[2]]

<p>31<sup>st</sup> 2015年3月</p>

你可以使用

圣1 < sup > < / sup > 2 < sup >和< / sup > 研发3 < sup > < / sup > 4 < sup >的< / sup >

用于定位后缀

通过将数字分成一个数组并反转,我们可以很容易地使用数组[0]和数组[1]检查数字的最后两位数字。

如果一个数字是十位数数组[1]= 1,它需要“th”。

function getDaySuffix(num)
{
    var array = ("" + num).split("").reverse(); // E.g. 123 = array("3","2","1")
    
    if (array[1] != "1") { // Number is not in the teens
        switch (array[0]) {
            case "1": return "st";
            case "2": return "nd";
            case "3": return "rd";
        }
    }
    
    return "th";
}
const getOrdinalNum = (n) => n + (n > 0 ? ['th', 'st', 'nd', 'rd'][(n > 3 && n < 21) || n % 10 > 3 ? 0 : n % 10] : '');