如何在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

其他回答

这是另一个,支持本地化:)

Date.prototype.getMonthName = function(lang) {
    lang = lang && (lang in Date.locale) ? lang : 'en';
    return Date.locale[lang].month_names[this.getMonth()];
};

Date.prototype.getMonthNameShort = function(lang) {
    lang = lang && (lang in Date.locale) ? lang : 'en';
    return Date.locale[lang].month_names_short[this.getMonth()];
};

Date.locale = {
    en: {
       month_names: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
       month_names_short: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
    }
};

然后,您可以轻松地添加对其他语言的支持:

Date.locale.fr = {month_names: [...]};

我是通过DatePipe完成的。

const datePipe = new DatePipe(this.locale);
datePipe.transform(myDate, 'MMM. y');

你可以像这样在构造函数中注入默认区域:

constructor(@Inject(LOCALE_ID) private locale: string){}

Angular的引用https://angular.io/api/common/DatePipe

我用了一个很有效的小窍门:

const monthNumber = 8; const yearNumber = 2018; const date = ' ${['Jan', 'Feb', 'Mar', 'Apr', “五月”,“六月”,“七月”,“八月”, 'Sep', 'Oct', 'Nov', 'Dec'][monthNumber - 1] } $ {yearNumber} '; console.log(日期);

对我来说,这是最佳解,

对于TypeScript也一样

const env = process.env.REACT_APP_LOCALE || 'en';

const namedMonthsArray = (index?: number): string[] | string => {
  const months = [];

  for (let month = 0; month <= 11; month++) {
    months.push(
      new Date(new Date('1970-01-01').setMonth(month))
        .toLocaleString(env, {
          month: 'long',
        })
        .toString(),
    );
  }
  if (index) {
    return months[index];
  }
  return months;
};

输出是

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

较短的版本:

const monthNames = ["January", "February", "March", "April", "May", "June", “七月”,“八月”,“九月”,“十月”,“十一月”,“十二月” ]; const d = new Date(); 文档。write("当前月份是" + monthNames[d.getMonth()]);

注(2019-03-08)-我最初在2009年写的这个答案已经过时了。请参阅David Storey的回答以获得更好的解决方案。