Get month name from Date

前端 未结 30 2750
情歌与酒
情歌与酒 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:51

    Here's another one, with support for localization :)

    Date.prototype.getMonthName = function(lang) {
        lang = lang && (lang in Date.locale) ? lang : 'en';
        return Date.locale[lang].month_names[this.getMonth()];
    };
    
    Date.prototype.getMonthNameShort = function(lang) {
        lang = lang && (lang in Date.locale) ? lang : 'en';
        return Date.locale[lang].month_names_short[this.getMonth()];
    };
    
    Date.locale = {
        en: {
           month_names: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
           month_names_short: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
        }
    };
    

    you can then easily add support for other languages:

    Date.locale.fr = {month_names: [...]};
    
    0 讨论(0)
  • 2020-11-22 00:52

    If you're using jQuery, you're probably also using jQuery UI, which means you can use $.datepicker.formatDate().

    $.datepicker.setDefaults( $.datepicker.regional[ "nl" ] );   // dutch
    $.datepicker.formatDate( "dd MM yy", objDate );
    
    0 讨论(0)
  • 2020-11-22 00:53

    The natural format this days is to use Moment.js.

    The way to get the month in a string format , is very simple in Moment.js no need to hard code the month names in your code: To get the current month and year in month name format and full year (May 2015) :

      moment(new Date).format("MMMM YYYY");
    
    0 讨论(0)
  • 2020-11-22 00:53

    If you don't want to use an external library, or store an array of month names, or if the ECMAScript Internationalization API is not good enough because of browser compatibility you can always do it the old-fashioned way by extracting the info from the date output:

    var now = new Date();
    var monthAbbrvName = now.toDateString().substring(4, 7);
    

    This would give you the abbreviated month name, e.g. Oct. I believe the date will come in all sorts of formats depending on the initialization and your locale so take a look at what toDateString() returns and recalculate your substring() values based on that.

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

    With momentjs, just use the format notation.

    const myDate = new Date()
    const shortMonthName = moment(myDate).format('MMM') // Aug
    const fullMonthName = moment(myDate).format('MMMM') // August
    
    0 讨论(0)
  • 2020-11-22 00:54

    Here's a way that does not depend on a hard-coded array and supports multiple locales.

    If you need a whole array:

    var monthsLocalizedArray = function(locale) {
        var result = [];
        for(var i = 0; i < 12; i++) {
            result.push(new Date(2010,i).toLocaleString(locale,{month:"long"}));
        }
        return result;
    };
    

    Usage:

    console.log(monthsLocalizedArray('en')); // -> ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
    console.log(monthsLocalizedArray('bg')); // -> ["януари", "февруари", "март", "април", "май", "юни", "юли", "август", "септември", "октомври", "ноември", "декември"]
    

    If you need only a selected month (faster):

    var monthLocalizedString = function(month, locale) {
        return new Date(2010,month).toLocaleString(locale,{month:"long"});
    };
    

    Usage:

    console.log(monthLocalizedString(1, 'en')); // -> February
    console.log(monthLocalizedString(1, 'bg')); // -> февруари
    console.log(monthLocalizedString(1, 'de')); // -> Februar
    

    Tested and works fine on Chrome and IE 11. On mozilla some modifications are needed, because it returns the whole date.

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