Get month name from Date

前端 未结 30 2920
情歌与酒
情歌与酒 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: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(","));
        }
    

提交回复
热议问题