Kinda是JavaScript的新功能,正在尝试向日期中添加序数信息,例如,取而代之的是4月4日,我希望当前的4月4日即时消息只是使用下面的代码来获取日期号,我将添加什么以获取序数信息

//Date
var d = new Date();
var n = d.getDate();
document.getElementById("date").innerHTML = n;

最佳答案

我相信这只是该序号英文短语的最右边两个字母。由于您只对1到31之间的数字感兴趣,因此我只将日期用作数组的索引:

var ordinalNames = [ "", "first", "second", "third", "fourth", "fifth", "sixth", "seventh", "eighth", "ninth", "tenth", "eleventh", "twelfth", "thirteenth", "fourteenth", "fifteenth", "sixteenth", "seventeenth", "eighteenth", "nineteenth", "twentieth", "twenty-first", "twenty-second", "twenty-third", "twenty-fourth", "twenty-fifth", "twenty-sixth", "twenty-seventh", "twenty-eighth", "twenty-ninth", "thirtieth", "thirty-first" ];

var d = new Date();
var n = d.getDate();
document.getElementById("date").innerHTML = n + ordinalNames[n].slice(-2);

09-18 10:43