在JavaScript中,我该如何从这个日期对象生成月份的名称(例如:Oct/October)?
var objDate = new Date("10/11/2009");
在JavaScript中,我该如何从这个日期对象生成月份的名称(例如:Oct/October)?
var objDate = new Date("10/11/2009");
现在可以使用ECMAScript国际化API来实现这一点:
const date = new Date(2009, 10, 10); // 2009-11-10
const month = date.toLocaleString('default', { month: 'long' });
console.log(month);
'long'
使用月份的全名,'short'
使用月份的缩写名称,'narrow'
使用最简版本,例如在字母语言中使用第一个字母。
您可以将浏览器的区域设置从 'default'
更改为任何您喜欢的设置(例如 'en-us'
),它将使用该语言/国家的正确名称。
使用 toLocaleString
api 每次都需要传递地区设置和选项。如果您要在多个不同日期上使用相同的区域设置和格式选项,则可以使用 Intl.DateTimeFormat
:
const formatter = new Intl.DateTimeFormat('fr', { month: 'short' });
const month1 = formatter.format(new Date());
const month2 = formatter.format(new Date(2003, 5, 12));
console.log(`${month1} and ${month2}`); // current month in French and "juin".
欲了解更多信息,请参阅我的博客文章:国际化API。
简短版本:
const monthNames = ["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"
];
const d = new Date();
document.write("The current month is " + monthNames[d.getMonth()]);
注(2019-03-08)- 我于2009年撰写的此答案已经过时。请查看David Storey的答案,以获得更好的解决方案。
new Date()
返回 Tue Sep 06 2011 20:02:25 GMT+0200 (CEST)
这明显说明日期对象已经内部定义了所有这些内容(月份和星期几的名称),但它并不是公开的,所以我们需要再次输入所有内容。 :((说明:本翻译保持原文意思,简化语言表达,并未添加解释或其他额外内容) - zanonaString#split
与 toString
或 toDateString
必有更好的方式。 - rxgx这里还有一个支持本地化的版本 :)
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: [...]};
Date.locale
是未定义的。但对于其他JS实现来说,这是一个非常好的答案! - mikemaccana如果我们需要传入输入,则需要使用以下方式
输入: '2020-12-28'
代码:
new Date('2020-12-28').toLocaleString('en-us',{month:'short', year:'numeric'})
输出:"2020年12月"
'en-us'
吗? - vinaltiday: 'numeric'
。 - RollyLuxon
和date-fns
,但是现在国际化API已经有了广泛的浏览器支持。 - Dan Dascalescu如果您不介意扩展日期原型(但有一些好的理由不希望这样做),实际上您可以想出一个非常简单的方法:
Date.prototype.monthNames = [
"January", "February", "March",
"April", "May", "June",
"July", "August", "September",
"October", "November", "December"
];
Date.prototype.getMonthName = function() {
return this.monthNames[this.getMonth()];
};
Date.prototype.getShortMonthName = function () {
return this.getMonthName().substr(0, 3);
};
// usage:
var d = new Date();
alert(d.getMonthName()); // "October"
alert(d.getShortMonthName()); // "Oct"
这些函数将适用于所有JavaScript日期对象。
如果您匆忙...那么这里就是您需要的!
const date = new Date(Date.now());
date.toLocaleString('en-US', {month: 'short'}); // {month:'long'}
期望输出:"Apr"
您可以直接使用Date.toLocaleDateString()方法,并将需要解析的日期作为参数传递
const event = new Date(Date.UTC(2012, 11, 20, 3, 0, 0));
const options = { year: 'numeric', month: 'short', day: 'numeric' };
console.log(event.toLocaleDateString('de-DE', options));
// expected output: Donnerstag, 20. Dezember 2012
console.log(event.toLocaleDateString('en-US', options));
// US format
// In case you only want the month
console.log(event.toLocaleDateString(undefined, { month: 'short'}));
console.log(event.toLocaleDateString(undefined, { month: 'long'}));
您可以在火狐文档中找到更多信息。
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();
document.write(new Date().toLocaleString('en-us',{month:'long', year:'numeric', day:'numeric'}))