如何在自定义 JQuery 中添加日期 original/suffix?

How to add date original/suffix in custom JQuery?

我有以下Javascript旨在修改日期格式,但是我想添加日期原始或后缀,例如"st","nd" , "rd", "th" 到每个结束日期数字。例如,假设我们的当前日期设置为 4 月 28 日,但我想将日期显示为 4 月 28 日

因此,如果有可能...如何将此样本编码添加到当前 javascript 字符串中?

var 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";   } }

当前 JS:

  // Blog date
    window.addEventListener('DOMContentLoaded', function () {
    document.querySelectorAll('.waddons-blog-meta').forEach(el => el.textContent = formatDateString(el.textContent))
    }, false);
    function formatDateString(s) {
      var months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
      var arr = s.split('/');
      return arr[1] + " " + months[arr[0] - 1];

      }

当前 HTML:

<div class="waddons-blog-meta">3/7/2020 - </div>

只需在代码中调用 nth 函数即可。

var 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";
      }
    }

window.addEventListener('DOMContentLoaded', function () {
  document.querySelectorAll('.waddons-blog-meta').forEach(el => el.textContent = formatDateString(el.textContent))
}, false);
function formatDateString(s) {
  var months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
  var arr = s.split('/');
  return arr[1] + nth(arr[1]) + " " + months[arr[0] - 1];
}

如果要在 th 值上标,请使用以下代码

window.addEventListener('DOMContentLoaded', function () {
  document.querySelectorAll('.waddons-blog-meta').forEach(el => el.innerHTML = formatDateString(el.textContent))
}, false);
function formatDateString(s) {
  var months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
  var arr = s.split('/');
  return arr[1] + "<sup>" + nth(arr[1]) + "</sup>" + " " + months[arr[0] - 1];
}