admin管理员组

文章数量:1296877

I'm struggling to make a extension on the date object to return the current month in name. It should return (January) if the method d.getMonthText() is called.

I've searched for extension online but I can't seem to figure it out.

I'm struggling to make a extension on the date object to return the current month in name. It should return (January) if the method d.getMonthText() is called.

I've searched for extension online but I can't seem to figure it out.

Share Improve this question asked Jan 11, 2017 at 13:35 MaximMaxim 971 silver badge9 bronze badges
Add a ment  | 

6 Answers 6

Reset to default 8

Just map date.getMonth() method value to month values

Date.prototype.getMonthText = function() {
  var months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
  return months[this.getMonth()];
}

var now = new Date();
var month = now.getMonthText();
console.log(month);

Although not remended, you can add methods to almost any javascript built–in object through its prototype property.

Date.prototype.getMonthName = function() {
    let months = [ 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December' ];

    return months[this.getMonth()];
};

Then just use it:

let date = new Date();
let month = date.getMonthName();

Just to add an option, you might consider toLocaleString which should support the ECMA 402 internationalisation API. It's not ubiquitous yet but worth considering for the future as it will return the month name in any language using the ISO 639-1 language codes:

Date.prototype.getMonthName = function(lang) {
  // Default language is English
  lang = lang || 'en-GB';
  return this.toLocaleString(lang, {month:'long'});
}

console.log(new Date().getMonthName())        // English (default)

console.log(new Date().getMonthName('ar-sy')) // Arabic

console.log(new Date().getMonthName('es-py')) // Spanish

there you go in a very basic way.. This function will return you the name of Month

<script>
function getMonthText(){
var month;
 switch (new Date().getMonth()) {
   case 0:
     month= "Jan";
     break;
   case 1:
     month= "Feb";
     break;
   case 2:
     month= "March";
     break;
   case 3:
     month= "April";
     break;
   case 4:
     month= "May";
     break;
   case 5:
     month= "June";
     break;
   case  6:
     month= "July";
     break;
   case  7:
     month= "August";
     break;
   case  8:
     month= "Sep";
     break;
   case  9:
     month= "Oct";
     break;
   case  10:
     month= "Nov";
     break;
   case  11:
     month= "Dec";
     break;
}}
 </script>

ECMAScript 6 version :

getPreviousMonth(monthBefore) {
    let monthList = [];
    let date = new Date();
    for (let i = 0; i < monthBefore; i++) {
        date.setMonth(date.getMonth() - 1);
        monthList.push(date.toLocaleString('en-us', {
            month: 'long',
        }));
    }
    return monthList;
}

You could monkey-patch the Date prototype object and implement an extension method:

Date.prototype.getMonthName = function() {
  return "January,February,March,April,May,June,July,August,September,October,November,December".split(",")[this.getMonth()];
}

本文标签: Date extension javascript get month nameStack Overflow