Get month name from Date

前端 未结 30 3708
情歌与酒
情歌与酒 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 01:06

    Just extending on the many other excellent answers - if you are using jQuery - you could just do something like

    $.fn.getMonthName = function(date) {
    
        var monthNames = [
        "January", "February", "March",
        "April", "May", "June",
        "July", "August", "September",
        "October", "November", "December"
        ];
    
        return monthNames[date.getMonth()];
    
    };
    

    where date is equal to the var d = new Date(somevalue). The primary advantage of this is per @nickf said about avoiding the global namespace.

提交回复
热议问题