gpt4 book ai didi

javascript - 添加迄今为止的序号信息

转载 作者:行者123 更新时间:2023-12-02 14:48:33 24 4
gpt4 key购买 nike

对 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);

关于javascript - 添加迄今为止的序号信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36388716/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com