Month Array in JavaScript Not Pretty

How can i make it nicer?

var month = new Array();

month['01']='Jan';
month['02']='Feb';
month['03']='Mar';

etc. Itd be nice to do it like:

var months = new Array(['01','Jan'],['02','Feb'],['03','Mar']);

For example. anyway like that to simplify it?

this should do it ..

var months = {'01':'Jan', '02':'Feb'};
alert( months['01'] );

JavaScript JS month names arrays · GitHub, const months = Array.from({length: 12}, (e, i) => { return new Date(null, i + 1, null).​toLocaleDateString("en", {month: "short"}); }). You can dynamically pass  JavaScript JS month names arrays. GitHub Gist: instantly share code, notes, and snippets.

For a more natural approach, try this little snippet. It works with Date objects and just as a regular function:

'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);

You can directly copy and paste this, then use it like this:

var today = new Date();
console.log(today.getLongMonth());
console.log(Date.getLongMonth(9));          // September
console.log(today.getShortMonth());
console.log(Date.getShortMonth('09'));      // Sept

This technique will provide flexibility as to how you index and how you access it. When using the Date object it will work correctly, but if using it as a standalone function it considers the months in human readable format from 1-12.

Fiddle with it!

JavaScript in 10 Simple Steps or Less, The day ofthe month is selected by using the value Of the currentMonth variable as the index for the months array. If the first day ofthe month is not Sunday, then  Just subtract one from the day-of-month: var today = new Date(); var yesterday = new Date().setDate(today.getDate() - 1); If the "date" property (the day-of-month) is 1, then setting it to zero will give you a date representing the last day of the previous month.

Short Dynamic Solution:

Here's a dynamic solution that does not require hard-coding an array of months:

const month = f=>Array.from(Array(12),(e,i)=>new Date(25e8*++i).toLocaleString('en-US',{month:f}));

Test Cases:

// Using Number Index:

month`long`[0];    // January
month`long`[1];    // February
month`long`[2];    // March

month`short`[0];   // Jan
month`short`[1];   // Feb
month`short`[2];   // Mar

month`narrow`[0];  // J
month`narrow`[1];  // F
month`narrow`[2];  // M

month`numeric`[0]; // 1
month`numeric`[1]; // 2
month`numeric`[2]; // 3

month`2-digit`[0]; // 01
month`2-digit`[1]; // 02
month`2-digit`[2]; // 03

// Using String Index:

let index_string = '01';

month`long`[index_string-1];    // January
month`short`[index_string-1];   // Jan
month`narrow`[index_string-1];  // J
month`numeric`[index_string-1]; // 1
month`2-digit`[index_string-1]; // 01

JavaScript: The Web Technologies Series, For example, the following code includes an array named months with 12 elements of the months of the year: <script type="text/javascript"> var today = new DateO; var months May not be copied, scanned, or duplicated, in Whole or in part. In the JavaScript Date object, when you call getMonth() it returns the month but it counts from 0. 0 = January 1 = February But when you call getDate() it starts counting from 1

why not:

var month = [
  'Jan', 
  'Feb', 
  // ...
  'Dec'];

To get the month name from the number you'd do something like:

var monthNum = 2; // February
var monthShortName = month[monthNum-1];

HTML5, JavaScript, and jQuery 24-Hour Trainer, JavaScript does not contain a library for formatting dates, although there are to place a textual representation of each month in an array at its relevant index. 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 code, but it's not working for me.

Don't use an Array unless you're using real numeric indexes. Try this:

var month = {
    '01': 'Jan',
    '02': 'Feb',
    // ...
    '12': 'Dec'
};

Personally, though, I would wrap this kind of logic in a function:

var monthNames = ['Jan', 'Feb', /* ... */ 'Dec'];
function getMonthName(n) {
    return monthNames(n - 1);
}

alert(getMonthName(1)); // 'Jan'

That way, you never have to think about the underlying data structure, or worry about changing it later.

JavaScript: The Web Warrior Series, of the day or month. For example, the following code includes an array named months with May not be copied, scanned, or duplicated, in whole or in part. And besides, starting array indices from 0 is a feature, not a bug. – Robusto May 13 '10 at 12:31 If you want to display indices to a user you can just display i + 1.

JavaScript: Get the month name from a particular date, JavaScript exercises, practice and solution: Write a JavaScript function to get the month name from a particular date. JavaScript: Tips of the Day. Create a n-​dimensional array with given value. Example: const tips_ndArray  JavaScript does not support associative arrays. You should use objects when you want the element names to be strings (text). You should use arrays when you want the element names to be numbers.

JavaScript getMonth() Method, Return the name of the month (not just a number):. var d = new Date(); var month = new Array(); month[0] = "January"; month[1] = "February"; month[2] = "March"; Is there any sort of "not in" operator in JavaScript to check if a property does not exist in an object? I couldn’t find anything about this around Google or Stack Overflow. Here’s a small snippet of code I’m working on where I need this kind of functionality:

Everything You Need to Know About Date in JavaScript, In JavaScript, if you want to use a date string, you need to use a Since Month is zero-indexed, we can use an array instead of an object. Definition and Usage. The getMonth() method returns the month (from 0 to 11) for the specified date, according to local time. Note: January is 0, February is 1, and so on.

Comments
  • To be clear you are creating an object not an array. You are actually defining the properties of month. For this reason you will not get what your expecting if you alert(month.length);
  • Sorry, i should have said, im creating a new array object...
  • Am I the only who came here just to copy-paste the month array from answers? ( because I am too lazy to write on my own )
  • damn it, duh, JSON... Thanks!
  • Oh, and ill update you as the right answer after I wait the 10 mins it says I have to wait...
  • Keep in mind that if you plan on using the Date object that months begin with 0 (i.e. Jan = 0, Feb =1)
  • This serves as a great base for further development. And thanks for the fiddle!
  • I knew I had to do this, and I was like, "Man, I don't want to have to type in all those names into an array! I bet one of our SO buddies has already done it." - and POW - I had a copy/paste in 15 seconds! LOL thanks for enabling my laziness :)
  • Imagine that, using numerical indices on an array object. Whatever next!?
  • Object indexes Andy, comon. var one = {'1': 'one'}; var month = {}; month[one] = "Jan";
  • Hahah Anurag I think I'm going to be sick
  • @Andy E - dude what happened to your head?!
  • @bears: horrific bear accident. If only I'd seen your screen name beforehand. Fortunately my head was intact - my body was just holding me back anyway ;-)