Get month name from Date

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

    Shorter version:

    const monthNames = ["January", "February", "March", "April", "May", "June",
      "July", "August", "September", "October", "November", "December"
    ];
    
    const d = new Date();
    document.write("The current month is " + monthNames[d.getMonth()]);

    Note (2019-03-08) - This answer by me which I originally wrote in 2009 is outdated. See David Storey's answer for a better solution.

提交回复
热议问题