month array javascript code example

Example 1: javascript months array

'use strict';

(function(d){
    var mL = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
    var mS = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'June', 'July', 'Aug', 'Sept', 'Oct', 'Nov', 'Dec'];

    d.prototype.getLongMonth = d.getLongMonth = function getLongMonth (inMonth) {
        return gM.call(this, inMonth, mL);
    }

    d.prototype.getShortMonth = d.getShortMonth = function getShortMonth (inMonth) {
        return gM.call(this, inMonth, mS);
    }

    function gM(inMonth, arr){
        var m;

        if(this instanceof d){
            m = this.getMonth();
        }
        else if(typeof inMonth !== 'undefined') {
            m = parseInt(inMonth,10) - 1; // Subtract 1 to start January at zero
        }

        return arr[m];
    }
})(Date);

Example 2: array month name

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

Example 3: month list javascript

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

Example 4: get the name of month without using hard coded arrays in javascript

//Assume that cuurent month is December results are given

var getmonth  = new Date().toLocaleString("en-us", { month: "short" });
console.log(getmonth);
//result =Dec


var getmonth  = new Date().toLocaleString("en-us", { month: "long" });
console.log(getmonth);

//result= December


var getmonth  = new Date().toLocaleString("en-us", { month: "narrow" });
console.log(getmonth);

//result=D