Get month name from Date

前端 未结 30 2882
情歌与酒
情歌与酒 2020-11-22 00:16

How can I generate the name of the month (e.g: Oct/October) from this date object in JavaScript?

var objDate = new Date(\"10/11/2009\");
相关标签:
30条回答
  • 2020-11-22 00:54

    For me this is best solution is,

    for TypeScript as well

    const env = process.env.REACT_APP_LOCALE || 'en';
    
    const namedMonthsArray = (index?: number): string[] | string => {
      const months = [];
    
      for (let month = 0; month <= 11; month++) {
        months.push(
          new Date(new Date('1970-01-01').setMonth(month))
            .toLocaleString(env, {
              month: 'long',
            })
            .toString(),
        );
      }
      if (index) {
        return months[index];
      }
      return months;
    };
    

    Output is

    ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
    
    0 讨论(0)
  • 2020-11-22 00:54

    If you don't want to use moment and want to display month name -

    .config($mdDateLocaleProvider) {
        $mdDateLocaleProvider.formatDate = function(date) {      
          if(date !== null) {
            if(date.getMonthName == undefined) {
              date.getMonthName = function() {
                var monthNames = [ "January", "February", "March", "April", "May", "June", 
                "July", "August", "September", "October", "November", "December" ];
                return monthNames[this.getMonth()];
              }
            }        
            var day = date.getDate();
            var monthIndex = date.getMonth();
            var year = date.getFullYear();
            return day + ' ' + date.getMonthName() + ' ' + year;
          }
        };
      }
    
    0 讨论(0)
  • 2020-11-22 00:55

    You can use one of several available Date formatters. Since this falls within the JavaScript specification, it will be available in both browser and server-side modes.

    objDate.toString().split(" ")[1]; // gives short name, unsure about locale 
    objDate.toLocaleDateString.split(" ")[0]; // gives long name
    

    e.g.

    js> objDate = new Date(new Date() - 9876543210)
    Mon Feb 04 2013 12:37:09 GMT-0800 (PST)
    js> objDate.toString().split(" ")[1]
    Feb
    js> objDate.toLocaleString().split(" ")[0]
    February
    

    There are more at https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date

    0 讨论(0)
  • 2020-11-22 00:56

    My Best Solution is as follow:

           var dateValue = Date();
           var month = dateValue.substring(4,7);
           var date = dateValue.substring(8,10);
           var year = dateValue.substring(20,24);
           var finaldateString = date+"-"+month+"-"+year;
    
    0 讨论(0)
  • 2020-11-22 00:57

    Some common easy process from date object can be done by this.

    var monthNames = ["January", "February", "March", "April", "May", "June",
      "July", "August", "September", "October", "November", "December"
    ];
    var monthShortNames = ["Jan", "Feb", "Mar", "Apr", "May", "Jun",
      "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
    ];
    
    function dateFormat1(d) {
      var t = new Date(d);
      return t.getDate() + ' ' + monthNames[t.getMonth()] + ', ' + t.getFullYear();
    }
    
    function dateFormat2(d) {
      var t = new Date(d);
      return t.getDate() + ' ' + monthShortNames[t.getMonth()] + ', ' + t.getFullYear();
    }
    
    console.log(dateFormat1(new Date()))
    console.log(dateFormat2(new Date()))

    Or you can make date prototype like

    Date.prototype.getMonthName = function() {
      var monthNames = ["January", "February", "March", "April", "May", "June",
        "July", "August", "September", "October", "November", "December"
      ];
      return monthNames[this.getMonth()];
    }
    
    
    Date.prototype.getFormatDate = function() {
      var monthNames = ["January", "February", "March", "April", "May", "June",
        "July", "August", "September", "October", "November", "December"
      ];
      return this.getDate() + ' ' + monthNames[this.getMonth()] + ', ' + this.getFullYear();
    }
    
    
    console.log(new Date().getMonthName())
    console.log(new Date().getFormatDate())

    Ex:

    var dateFormat3 = new Date().getMonthName(); # March

    var dateFormat4 = new Date().getFormatDate(); # 16 March, 2017

    0 讨论(0)
  • 2020-11-22 00:58

    You might use datejs to do that. Check the FormatSpecifiers, MMMM gives you the month's name:

    var objDate = new Date("10/11/2009");
    document.write(objDate.toString("MMMM"));
    

    And datejs got that localized for more than 150 locales! See here

    0 讨论(0)
提交回复
热议问题