JavaScript new Date Ordinal (st, nd, rd, th)

前端 未结 15 1856
北海茫月
北海茫月 2020-11-27 03:12

If at all possible, without JavaScript libraries or lots of clunky code I am looking for the simplest way to format a date two weeks from now in the following format:

<
相关标签:
15条回答
  • 2020-11-27 03:44

    I was doing this for dates as well, but because the day of month can only be between 1 and 31, I ended up with a simplified solution.

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

    or compact version using conditional operators

    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

    0 讨论(0)
  • 2020-11-27 03:45

    Here:

    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>

    0 讨论(0)
  • 2020-11-27 03:48

    Here is a one liner inspired by the other answers. It is tested and will take 0 and negative numbers.

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

    Update 2020-06-23. The following is a better readable answer of the function above:

    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];
    };
    
    
    
    0 讨论(0)
  • 2020-11-27 03:49

    One more solution in the sea of solutions.

    let suffix = (day >= 4 &&  day <= 20) || (day >= 24 && day <= 30)
        ? "th"
        : ["st", "nd", "rd"][day % 10 - 1];
    
    0 讨论(0)
  • 2020-11-27 03:50

    Here is an easy solution:

    var date = today.getDate() + (today.getDate() % 10 == 1 && today.getDate() != 11 ? + 'st': (today.getDate() % 10 == 2 && today.getDate() != 12 ? + 'nd': 
    
    (today.getDate() % 10 == 3 && today.getDate() != 13 ? + 'rd':'th')));
    
    0 讨论(0)
  • 2020-11-27 03:53

    Super simple functional implementation:

    const ordinal = (d) => {
      const nth = { '1': 'st', '2': 'nd', '3': 'rd' }
      return `${d}${nth[d] || 'th'}`
    }
    
    const monthNames = ['January','February','March','April','May','June','July','August','September','October','November','December']
    
    const dateString = (date) => `${ordinal(date.getDate())} ${monthNames[date.getMonth()]} ${date.getFullYear()}`
    
    // Use like this: 
    dateString(new Date()) // 18th July 2016
    
    0 讨论(0)
提交回复
热议问题