JavaScript 新日期序数(st、nd、rd、th)

IT技术 javascript date ordinal-indicator
2021-02-10 14:19:13

如果可能的话,没有 JavaScript 库或大量笨重的代码,我正在寻找最简单的方法来格式化两周后的日期,格式如下:

13th March 2013

我正在使用的代码是:

var newdate = new Date(+new Date + 12096e5);
document.body.innerHTML = newdate;

它返回两周后的日期和时间,但像这样:Wed Mar 27 2013 21:50:29 GMT+0000(GMT 标准时间)

这是jsFiddle 中的代码

任何帮助,将不胜感激!

6个回答

这里:

JSFiddle

const nth = function(d) {
  if (d > 3 && d < 21) return 'th';
  switch (d % 10) {
    case 1:  return "st";
    case 2:  return "nd";
    case 3:  return "rd";
    default: return "th";
  }
}

const fortnightAway = new Date(+new Date + 12096e5);
const date = fortnightAway.getDate();
const month = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"][fortnightAway.getMonth()];

document.getElementById("date").innerHTML = `In two weeks it will be the ${date}<sup>${nth(date)}</sup> ${month} ${fortnightAway.getFullYear()}`;

// test
const dates = [...Array(32).keys()].slice(1).map(i => `${i}${nth(i)}`)
console.log(dates.join(", "))
sup {
  font-size: x-small
}
<span id="date"></span>

完美的作品... +1 d>3 && d<21
2021-03-27 14:19:13
对不起?不明白你的意思
2021-04-03 14:19:13
为清楚起见,我可能会将异常编码为: if (n == 11 || n == 12 || n == 13) return "th"; 或: if (n >= 11 && n <= 13) 返回“th”;
2021-04-04 14:19:13
序数函数(压缩): function nth(n){return n>3&&n<21?"th":n%10==2?"nd":n%10==2?"nd":n%10==3?"rd":"th"}
2021-04-04 14:19:13
那就是缺少 21,22,23
2021-04-06 14:19:13

这是一个受其他答案启发的单线。它经过测试,将采用 0 和负数。

function getOrdinalNum(n) {
  return n + (n > 0 ? ['th', 'st', 'nd', 'rd'][(n > 3 && n < 21) || n % 10 > 3 ? 0 : n % 10] : '');
}

更新 2020-06-23。以下是上述函数的可读性更好的答案:

const getOrdinalNum = (number) => {
  let selector;

  if (number <= 0) {
    selector = 4;
  } else if ((number > 3 && number < 21) || number % 10 > 3) {
    selector = 0;
  } else {
    selector = number % 10;
  }

  return number + ['th', 'st', 'nd', 'rd', ''][selector];
};


这不适用于 111、112、113、211 等。这可能适用于所有人。 ["th", "st", "nd", "rd"][n % 100 > 10 && n % 100 < 14) || n % 10 > 3 ? 0 : n % 10] 但日期没有这种情况。如果有人需要将它用于任何正整数
2021-04-10 14:19:13

我也在为日期做这件事,但是因为一个月中的某一天只能在 1 到 31 之间,所以我最终得到了一个简化的解决方案。

function dateOrdinal(dom) {
    if (dom == 31 || dom == 21 || dom == 1) return dom + "st";
    else if (dom == 22 || dom == 2) return dom + "nd";
    else if (dom == 23 || dom == 3) return dom + "rd";
    else return dom + "th";
};

或使用条件运算符的紧凑版本

function dateOrdinal(d) {
    return d+(31==d||21==d||1==d?"st":22==d||2==d?"nd":23==d||3==d?"rd":"th")
};

http://jsben.ch/#/DrBpl

很多格式化答案,所以我只会处理任何整数的第 n 个 -

Number.prototype.nth= function(){
    if(this%1) return this;
    var s= this%100;
    if(s>3 && s<21) return this+'th';
    switch(s%10){
        case 1: return this+'st';
        case 2: return this+'nd';
        case 3: return this+'rd';
        default: return this+'th';
    }
}
为什么this%1Mod 1 始终为 0,因此该语句将始终失败,不是吗?
2021-03-25 14:19:13
if 是针对青少​​年的,所以 13 是第 13 个而不是第 13 个。
2021-04-02 14:19:13

这是一个适用于任何数字的简单函数:

function getOrdinal(n) {
    let ord = ["st", "nd", "rd"]
    let exceptions = [11, 12, 13]
    let nth = 
    ord[(n % 10) - 1] == undefined || exceptions.includes(n % 100) ? "th" : ord[(n % 10) - 1]
    return n + nth
}

它可以接受数字或数字作为字符串。例如:

getOrdinal(28)        //Outputs: 28th
getOrdinal('108')     //Outputs: 108th