Get month name from Date

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

    Unfortunately the best way to extract the month name is from the UTCString representation:

    Date.prototype.monthName = function() {
        return this.toUTCString().split(' ')[2]
    };
    
    d = new Date();
    //=> Thu Mar 06 2014 23:05:21 GMT+0000 (GMT)
    
    d.monthName();
    //=> 'Mar'
    
    0 讨论(0)
  • 2020-11-22 00:46

    This can be also done if you are using kendo.

    kendo.toString(dateobject, "MMMM");
    

    Here are list of formatters from kendo site:

    "d" Renders the day of the month, from 1 through 31.

    "dd" The day of the month, from 01 through 31.

    "ffffd" The abbreviated name of the day of the week.

    "ffffdd" The full name of the day of the week.

    "f" The tenths of a second in a date and time value.

    "ff" The hundredths of a second in a date and time value.

    "fff" The milliseconds in a date and time value.

    "M" The month, from 1 through 12.

    "MM" The month, from 01 through 12.

    "MMM" The abbreviated name of the month.

    "MMMM" The full name of the month.

    "h" The hour, using a 12-hour clock from 1 to 12.

    "hh" The hour, using a 12-hour clock from 01 to 12.

    "H" The hour, using a 24-hour clock from 1 to 23.

    "HH" The hour, using a 24-hour clock from 01 to 23.

    "m" The minute, from 0 through 59.

    "mm" The minute, from 00 through 59.

    "s" The second, from 0 through 59.

    "ss" The second, from 00 through 59.

    "tt" The AM/PM designator.

    "yy" The last two characters from the year value.

    "yyyy" The year full value.

    "zzz" The local timezone when using formats to parse UTC date strings.

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

    Instead of declaring array which hold all the month name and then pointing with an index, we can also write it in a shorter version as below:

    var objDate = new Date().toLocaleString("en-us", { month: "long" }); // result: August
    var objDate = new Date().toLocaleString("en-us", { month: "short" }); // result: Aug
    
    0 讨论(0)
  • 2020-11-22 00:47

    Another way to format date

    new Date().toLocaleString('en-us',{month:'long', year:'numeric', day:'numeric'}) //output: "May 21, 2019"
    
    0 讨论(0)
  • 2020-11-22 00:50

    It is now possible to do this with the ECMAScript Internationalization API:

    const date = new Date(2009, 10, 10);  // 2009-11-10
    const month = date.toLocaleString('default', { month: 'long' });
    console.log(month);

    'long' uses the full name of the month, 'short' for the short name, and 'narrow' for a more minimal version, such as the first letter in alphabetical languages.

    You can change the locale from browser's 'default' to any that you please (e.g. 'en-us'), and it will use the right name for that language/country.

    With toLocaleStringapi you have to pass in the locale and options each time. If you are going do use the same locale info and formatting options on multiple different dates, you can use Intl.DateTimeFormat instead:

    const formatter = new Intl.DateTimeFormat('fr', { month: 'short' });
    const month1 = formatter.format(new Date());
    const month2 = formatter.format(new Date(2003, 5, 12));
    console.log(`${month1} and ${month2}`); // current month in French and "juin".

    For more information see my blog post on the Internationalization API.

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

    I have a partial solution that I came up with. It uses a regular expression to extract the month and day name. But as I look through the Region and Language options (Windows) I realize that different cultures have different format order... maybe a better regular expression pattern could be useful.

    function testDateInfo() {
            var months = new Array();
            var days = new Array();
            var workingDate = new Date();
            workingDate.setHours(0, 0, 0, 0);
            workingDate.setDate(1);
            var RE = new RegExp("([a-z]+)","ig");
            //-- get day names 0-6
            for (var i = 0; i < 7; i++) {
    
                var day = workingDate.getDay();
                //-- will eventually be in order
                if (days[day] == undefined)
                    days[day] = workingDate.toLocaleDateString().match(RE)[0];
                workingDate.setDate(workingDate.getDate() + 1);
            }
            //--get month names 0-11
            for (var i = 0; i < 12; i++) {
                workingDate.setMonth(i);
                months.push(workingDate.toLocaleDateString().match(RE)[1]);
            }
            alert(days.join(",") + " \n\r " + months.join(","));
        }
    
    0 讨论(0)
提交回复
热议问题