Get month name from Date

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

    You could just simply use Date.toLocaleDateString() and parse the date wanted as parameter

    const event = new Date(Date.UTC(2012, 11, 20, 3, 0, 0));
    
    const options = {  year: 'numeric', month: 'short', day: 'numeric' };
    
    console.log(event.toLocaleDateString('de-DE', options));
    // expected output: Donnerstag, 20. Dezember 2012
    
    console.log(event.toLocaleDateString('en-US', options));
    // US format 
    
    
    // In case you only want the month
    console.log(event.toLocaleDateString(undefined, { month: 'short'}));
    console.log(event.toLocaleDateString(undefined, { month: 'long'}));

    You can find more information in the Firefox documentation

提交回复
热议问题