如何在JavaScript中从这个日期对象生成月份的名称(例如:10月/ 10月)?

var objDate = new Date("10/11/2009");

当前回答

date对象中一些常见的简单处理可以通过它来完成。

var monthNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ]; var monthShortNames = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" ]; function dateFormat1(d) { var t = new Date(d); return t.getDate() + ' ' + monthNames[t.getMonth()] + ', ' + t.getFullYear(); } function dateFormat2(d) { var t = new Date(d); return t.getDate() + ' ' + monthShortNames[t.getMonth()] + ', ' + t.getFullYear(); } console.log(dateFormat1(new Date())) console.log(dateFormat2(new Date()))

或者你可以制作日期原型

Date.prototype.getMonthName = function() { var monthNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ]; return monthNames[this.getMonth()]; } Date.prototype.getFormatDate = function() { var monthNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ]; return this.getDate() + ' ' + monthNames[this.getMonth()] + ', ' + this.getFullYear(); } console.log(new Date().getMonthName()) console.log(new Date().getFormatDate())

Ex:

var dateentt3新日期()#发情

var dateFormat4 = new Date().getFormatDate();# 16 三月, 2017

其他回答

将名称存储在一个数组中,并根据月份的索引进行查找。

var month=new Array(12);
month[0]="January";
month[1]="February";
month[2]="March";
month[3]="April";
month[4]="May";
month[5]="June";
month[6]="July";
month[7]="August";
month[8]="September";
month[9]="October";
month[10]="November";
month[11]="December";

document.write("The current month is " + month[d.getMonth()]);

JavaScript getMonth()方法

date对象中一些常见的简单处理可以通过它来完成。

var monthNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ]; var monthShortNames = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" ]; function dateFormat1(d) { var t = new Date(d); return t.getDate() + ' ' + monthNames[t.getMonth()] + ', ' + t.getFullYear(); } function dateFormat2(d) { var t = new Date(d); return t.getDate() + ' ' + monthShortNames[t.getMonth()] + ', ' + t.getFullYear(); } console.log(dateFormat1(new Date())) console.log(dateFormat2(new Date()))

或者你可以制作日期原型

Date.prototype.getMonthName = function() { var monthNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ]; return monthNames[this.getMonth()]; } Date.prototype.getFormatDate = function() { var monthNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ]; return this.getDate() + ' ' + monthNames[this.getMonth()] + ', ' + this.getFullYear(); } console.log(new Date().getMonthName()) console.log(new Date().getFormatDate())

Ex:

var dateentt3新日期()#发情

var dateFormat4 = new Date().getFormatDate();# 16 三月, 2017

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

它可以被用作

var month_Name = new Date().getMonthName();

如果您不想使用时刻,并希望显示月份名称-

.config($mdDateLocaleProvider) {
    $mdDateLocaleProvider.formatDate = function(date) {      
      if(date !== null) {
        if(date.getMonthName == undefined) {
          date.getMonthName = function() {
            var monthNames = [ "January", "February", "March", "April", "May", "June", 
            "July", "August", "September", "October", "November", "December" ];
            return monthNames[this.getMonth()];
          }
        }        
        var day = date.getDate();
        var monthIndex = date.getMonth();
        var year = date.getFullYear();
        return day + ' ' + date.getMonthName() + ' ' + year;
      }
    };
  }

现在可以使用ECMAScript国际化API来做到这一点:

const date = new date (2009,10,10);/ / 2009-11-10 Const month =日期。toLocaleString('default', {month: 'long'}); console.log(月);

“long”使用月份的全称,“short”使用简短的名称,“narrow”使用更简洁的版本,比如字母语言中的第一个字母。

你可以将浏览器的“默认”更改为任何你喜欢的语言环境(例如:'en-us'),它将为该语言/国家使用正确的名称。

使用toLocaleStringapi,你每次都必须传递locale和选项。如果你要在多个不同的日期使用相同的语言环境信息和格式选项,你可以使用Intl。DateTimeFormat相反:

const formatter = new Intl。DateTimeFormat('fr', {month: 'short'}); Const month1 =格式化程序。格式(新日期()); Const month2 = formatter。format(new Date(2003,5,12)); Console.log (' ${month1}和${month2} ');//当前月份的法语和“juin”。

有关更多信息,请参阅我关于国际化API的博客文章。