Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get list of months javascript [closed]

I'm trying to get the list of months in javascript! how can I get this done by using only javascript!

Thank you!

like image 250
Imesh Chandrasiri Avatar asked Jun 04 '13 03:06

Imesh Chandrasiri


People also ask

How to get list of months in JavaScript?

months() // returns a list of months in the current locale (January, February, etc.) moment. monthsShort() // returns abbreviated month names (Jan, Feb, etc.)

How to extract month from date in js?

JavaScript Date getMonth() getMonth() returns the month (0 to 11) of a date.

What does the getMonth() method of the date object return?

The getMonth() method returns the month in the specified date according to local time, as a zero-based value (where zero indicates the first month of the year).

How do I get the last Sunday of the month in JavaScript?

A function to get the last Sunday of a month might be: /** * Returns last Sunday of month * * @param {Temporal. YearMonth} queryMonth - YearMonth to get last Sunday of * @returns {Temporal. Date} for last Sunday of queried month */ function getLastSunday(queryMonth) { let lastOfMonth = queryMonth.


2 Answers

As far as I know, you can only get the array by hard-coding it.

var monthNames = [ "January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December" ];

Or you can use some javascript library which has this list hard-coded.

like image 182
Rong Nguyen Avatar answered Oct 17 '22 19:10

Rong Nguyen


Since you should be using moment to deal with dates anyway, you might as well use it here too! ;)

moment.months() or moment.monthsShort() (both added in 2.3.0):

const moment = require('moment');
moment.locale('en'); // sets words language (optional if current locale is to be used)
moment.months() // returns a list of months in the current locale (January, February, etc.)
moment.monthsShort() // returns abbreviated month names (Jan, Feb, etc.)
like image 30
Andbdrew Avatar answered Oct 17 '22 19:10

Andbdrew