Get month name from Date

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

    You can use one of several available Date formatters. Since this falls within the JavaScript specification, it will be available in both browser and server-side modes.

    objDate.toString().split(" ")[1]; // gives short name, unsure about locale 
    objDate.toLocaleDateString.split(" ")[0]; // gives long name
    

    e.g.

    js> objDate = new Date(new Date() - 9876543210)
    Mon Feb 04 2013 12:37:09 GMT-0800 (PST)
    js> objDate.toString().split(" ")[1]
    Feb
    js> objDate.toLocaleString().split(" ")[0]
    February
    

    There are more at https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date

提交回复
热议问题