$.date.months

Months method of date helpers

You may want to get only month names, so you can use this method, months method only have 2 parameters such as locale, short/long/narrow. Locale is a regional language code (e.g. en-US, in-ID, etc.), short is 3 characters of all month names, long is full month name, and narrow is 1 character month name.

// without arguments
$.date.months()
//=> ['January', 'February', 'March', 'April', 'May', 'June', 
// 'July', 'August', 'September', 'October', 'November', 
// 'December']

// with locale/language code
$.date.months('id-ID')
//=> ['Januari', 'Februari', 'Maret', 'April', 'Mei', 'Juni', 
// 'Juli', 'Agustus', 'September', 'Oktober', 'November',  
// 'Desember']

// shorted all month names
$.date.months('en-US', 'short')
//=> ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 
// 'Sep', 'Oct', 'Nov', 'Dec']

Also, maybe you want to get only one month name, so we provide another method which is month (without s) method, in this method only have 4 parameters such as year (actually we don't need year for this), month, locale and month type (long/short/narrow).

$.date.month(2021, 6, 'id-ID') //=> 'Juni'

If you want to create a month helper manually, you can create it by yourself and you can use all the code below.

function months(year = 2021, locale = 'en-US', monthType = 'long') {
  const arrayOfMap = []
  for(let i = 0; i <= 11; i++) {
    const utc = new Date(Date.UTC(year, i, 0))
    const format = new Intl.DateTimeFormat(
      locale,
      {
        month: monthType
      }
    ).format(utc)
    arrayOfMap.push([format, utc.getDate()])
  }
  return arrayOfMap
}

months(2020, 'en-US', 'short')
//=> [["Dec",31],["Jan",31],["Feb",29],["Mar",31],["Apr",30],
// ["May",31],["Jun",30],["Jul",31],["Aug",31],["Sep",30],
// ["Oct",31],["Nov",30]]

Last updated