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

前端 未结 15 1868
北海茫月
北海茫月 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:31

    I'm a bit late to the party, but this should work:

    function ordinal(number) {
      number = Number(number)
      if(!number || (Math.round(number) !== number)) {
        return number
      }
      var signal = (number < 20) ? number : Number(('' + number).slice(-1))
      switch(signal) {
        case 1:
          return number + 'st'
        case 2:
          return number + 'nd'
        case 3:
          return number + 'rd'
        default:
          return number + 'th'
      }
    }
    
    function specialFormat(date) {
      // add two weeks
      date = new Date(+date + 12096e5)
      var months = [
        'January'
        , 'February'
        , 'March'
        , 'April'
        , 'May'
        , 'June'
        , 'July'
        , 'August'
        , 'September'
        , 'October'
        , 'November'
        , 'December'
      ]
      var formatted = ordinal(date.getDate())
      formatted += ' ' + months[date.getMonth()]
      return formatted + ' ' + date.getFullYear()
    }
    
    document.body.innerHTML = specialFormat(new Date())
    
    0 讨论(0)
  • 2020-11-27 03:34

    Lots of formatting answers, so I'll just work on the nth of any integer-

    Number.prototype.nth= function(){
        if(this%1) return this;
        var s= this%100;
        if(s>3 && s<21) return this+'th';
        switch(s%10){
            case 1: return this+'st';
            case 2: return this+'nd';
            case 3: return this+'rd';
            default: return this+'th';
        }
    }
    
    0 讨论(0)
  • 2020-11-27 03:36

    A short and compact solution:

    function format(date, tmp){
      return [
        (tmp = date.getDate()) + 
          ([, 'st', 'nd', 'rd'][/1?.$/.exec(tmp)] || 'th'),
        [ 'January', 'February', 'March', 'April',
          'May', 'June', 'July', 'August',
          'September', 'October', 'November', 'December'
        ][date.getMonth()],
        date.getFullYear()
      ].join(' ')
    }
    
    
    // 14 days from today
    
    console.log('14 days from today: ' + 
      format(new Date(+new Date + 14 * 864e5)));
    
    // test formatting for all dates within a month from today
    
    var day = 864e5, today = +new Date;
    for(var i = 0; i < 32; i++) {
      console.log('Today + ' + i + ': ' + format(new Date(today + i * day)))
    }

    (The compact regex-based approach for getting the ordinal suffix appears several places around the web, original source unknown)

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

    Strongly inspired by @user2309185's.

    const ordinal = (d) => {
      return d + (['st', 'nd', 'rd'][d % 10 - 1] || 'th')
    }
    
    0 讨论(0)
  • 2020-11-27 03:39

    Lots of answers, here's another:

    function addOrd(n) {
      var ords = [,'st','nd','rd'];
      var ord, m = n%100;
      return n + ((m > 10 && m < 14)? 'th' : ords[m%10] || 'th');
    }
    
    // Return date string two weeks from now (14 days) in 
    // format 13th March 2013
    function formatDatePlusTwoWeeks(d) {
      var months = ['January','February','March','April','May','June',
                    'July','August','September','October','November','December'];
    
      // Copy date object so don't modify original
      var e = new Date(d);
    
      // Add two weeks (14 days)
      e.setDate(e.getDate() + 14);
      return addOrd(e.getDate()) + ' ' + months[e.getMonth()] + ' ' + e.getFullYear();
    }
    
    alert(formatDatePlusTwoWeeks(new Date(2013,2,13))); // 27th March 2013
    
    0 讨论(0)
  • 2020-11-27 03:39

    If you are a fan of moment.js, then you can make it with format("Do")

    Examples

    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
    
    0 讨论(0)
提交回复
热议问题