Get all months name from year in moment.js

后端 未结 9 912
执笔经年
执笔经年 2020-12-14 07:31

I want to get all months name from year in moment.js

if the year is 2011, then i want to all months name in momentjs

i have tried this below

相关标签:
9条回答
  • 2020-12-14 08:03

    It's 2019 and you shouldn't be using moment.js anymore, since the vanilla Date() class has everything we need. Except for this little bit of functionality.

    Here's what I use: There's the months NPM module which has all months in different languages. It's about 991 bytes gzipped.

    Example with proper import syntax:

    import months from 'months'
    
    console.log(months.de)
    

    Result:

    [
      "Januar",
      "Februar",
      "März",
      "April",
      "Mai",
      "Juni",
      "Juli",
      "August",
      "September",
      "Oktober",
      "November",
      "Dezember"
    ]
    
    0 讨论(0)
  • 2020-12-14 08:04

    If you want to list month names in a select in React:

      <select
        value={value}
        onChange={handleChange}>
    
             {moment.months().map(item => (
                <option key={item}>{item}</option>
             ))}
    
      </select>
    
    0 讨论(0)
  • 2020-12-14 08:08

    Using moment.js you have the following methods:

    moment.months() // long names
    

    returns:

    [ 'January',
      'February',
      'March',
      'April',
      'May',
      'June',
      'July',
      'August',
      'September',
      'October',
      'November',
      'December' ]
    

    and

    moment.monthsShort() // short names
    

    returns:

    ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
    
    0 讨论(0)
提交回复
热议问题