use*_*828 38 javascript date ordinal-indicator
如果可能的话,没有JavaScript库或许多笨重的代码,我正在寻找最简单的方法来格式化一个日期,从现在开始以下列格式:
13th March 2013
我使用的代码是:
var newdate = new Date(+new Date + 12096e5);
document.body.innerHTML = newdate;
Run Code Online (Sandbox Code Playgroud)
从现在起两周后返回日期和时间,但是像这样:2013年3月27日星期三21:50:29 GMT + 0000(格林威治标准时间标准时间)
这是jsFiddle中的代码.
任何帮助,将不胜感激!
mpl*_*jan 72
这里:
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 = `${date}<sup>${nth(date)}</sup> ${month} ${fortnightAway.getFullYear()}`;Run Code Online (Sandbox Code Playgroud)
小智 19
这是一个受其他答案启发的单线.它经过测试,将采用0和负数.
function getOrdinalNum(n) {
return n + (n > 0 ? ['th', 'st', 'nd', 'rd'][(n > 3 && n < 21) || n % 10 > 3 ? 0 : n % 10] : '');
}
Run Code Online (Sandbox Code Playgroud)
Ane*_*eed 10
如果你是 moment.js 的粉丝,那么你可以使用 format("Do") 来制作
例子
var newdate = new Date();
moment(newdate).format("Do MMMM YYYY")
//Returns 1st January 2020
moment("01/01/2020", "MM/DD/YYYY").format("Do")
//Returns 1st
moment("01/01/2020", "MM/DD/YYYY").format("Do MMM YYYY")
//Returns 1st Jan 2020
Run Code Online (Sandbox Code Playgroud)
我也在为日期做这件事,但是因为一个月中的某一天只能在 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";
};
Run Code Online (Sandbox Code Playgroud)
或使用条件运算符的紧凑版本
function dateOrdinal(d) {
return d+(31==d||21==d||1==d?"st":22==d||2==d?"nd":23==d||3==d?"rd":"th")
};
Run Code Online (Sandbox Code Playgroud)
这是一个适用于任何数字的简单函数:
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
}
Run Code Online (Sandbox Code Playgroud)
它可以接受数字或数字作为字符串。例如:
getOrdinal(28) //Outputs: 28th
getOrdinal('108') //Outputs: 108th
Run Code Online (Sandbox Code Playgroud)
这里已经有很多好的答案,尽管使用Intl.PluralRules的答案可能仍然有用,该答案标准化了跨区域设置的序数分类。
下面是 的一些实现en-GB。
一个衬垫:
console.log({one:'st',two:'nd',few:'rd',other:'th'}[new Intl.PluralRules('en-GB', { type: 'ordinal' }).select(new Date().getDate())])
Run Code Online (Sandbox Code Playgroud)
一个详细的例子:
const suffixMap = {
one: 'st',
two: 'nd',
few: 'rd',
other: 'th',
};
const locale = 'en-GB';
const moment = new Date();
const dayOfMonth = moment.getDate();
const pluralRuleOptions = {
type: 'ordinal',
};
const pluralRule = new Intl.PluralRules(locale, pluralRuleOptions);
const ordinal = pluralRule.select(dayOfMonth);
console.log(suffixMap[ordinal])
Run Code Online (Sandbox Code Playgroud)
const suffix = {
one: 'st',
two: 'nd',
few: 'rd',
other: 'th',
};
document.getElementById("tomorrow-month").innerHTML = new Intl.DateTimeFormat('en-GB', { month: 'long' }).format(new Date(Date.now() + 86400000));
document.getElementById("tomorrow-day").innerHTML = new Intl.DateTimeFormat('en-GB', { day: 'numeric' }).format(new Date(Date.now() + 86400000));
document.getElementById("tomorrow-ordinal").innerHTML = suffix[new Intl.PluralRules('en-GB', { type: 'ordinal' }).select(new Date(Date.now() + 86400000).getDate())];
document.getElementById("yesterday-month").innerHTML = new Intl.DateTimeFormat('en-GB', { month: 'long' }).format(new Date(Date.now() - 86400000));
document.getElementById("yesterday-day").innerHTML = new Intl.DateTimeFormat('en-GB', { day: 'numeric' }).format(new Date(Date.now() - 86400000));
document.getElementById("yesterday-ordinal").innerHTML = suffix[new Intl.PluralRules('en-GB', { type: 'ordinal' }).select(new Date(Date.now() - 86400000).getDate())];Run Code Online (Sandbox Code Playgroud)
Tomorrow, on <span id="tomorrow-month"></span> <span id="tomorrow-day"></span><sup id="tomorrow-ordinal"></sup>, I ordered a time machine.
It arrived yesterday on <span id="yesterday-month"></span> <span id="yesterday-day"></span><sup id="yesterday-ordinal"></sup>.Run Code Online (Sandbox Code Playgroud)
解决方案的海洋中又多了一个解决方案。
let suffix = (day >= 4 && day <= 20) || (day >= 24 && day <= 30)
? "th"
: ["st", "nd", "rd"][day % 10 - 1];
Run Code Online (Sandbox Code Playgroud)