Is there a built-in function or plugin to handle date formatting in JavaScript?

后端 未结 3 1499
眼角桃花
眼角桃花 2020-11-30 14:40

Currently I need to output a date in a: \'5 October, 2012\' type format. Meaning day-of-month with no leading zeros, space, full month name, comma, space, four-digit year. I

3条回答
  •  一整个雨季
    2020-11-30 15:19

    There are lots of date libraries, but if all you want to do is generate one specific format from a date object, it's pretty trivial:

    var formatDate = (function() {
    
      var months = ['January','February','March','April','May','June',
          'July','August','September','October','November','December'];
    
      return function(date) {
        return date.getDate() + ' ' + months[date.getMonth()] + ', ' + date.getFullYear();
      }
    }());
    
    alert(formatDate(new Date())); // 28 September, 2012
    

    I think that took less than 5 minutes.

提交回复
热议问题