如何在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原型(并且有一些很好的理由不想这样做),您实际上可以提出一个非常简单的方法:

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 Date对象。

最简单最简单的方法:

const dateStr =新的日期(2020,03,10).toDateString();//“2020年4月10日星期五” const dateStrArr = dateStr。分割(' ');// ['Fri', 'Apr', '10', '2020'] console.log (dateStrArr [1]);/ / 4月的

将日期转换为字符串。 间隔一个空格。 从数组中选择第二个元素。

在IE 11, Chrome, Firefox上测试

const dt = new Date(); Const locale = navigator。语言!= undefined ?导航器。[0]: navigator.language; const fullMonth = dt。toLocaleDateString(区域设置,{month: 'long'}); console.log (fullMonth);

如果你不想使用一个外部库,或者存储一个月份名称的数组,或者如果ECMAScript国际化API因为浏览器兼容性而不够好,你可以通过从日期输出中提取信息来实现:

var now = new Date();
var monthAbbrvName = now.toDateString().substring(4, 7);

这将为您提供缩写的月份名称,例如october。我相信日期将以各种格式出现,这取决于初始化和您的地区,因此请查看toDateString()返回的内容,并基于此重新计算您的substring()值。

你可以使用或不使用当地语言翻译

创造价值“2009年10月11日”

const objDate =新日期("10/11/2009"); const月=[‘简’,2月,3月,4月,“可能”,“君”,7月,8月,9月,10月,11月,12月的) if (objDate !== '无效日期' && !isNaN(objDate)) { console.log(objDate.getDate() + ' ' + months[objDate.getMonth()] + ' ' + objDate.getFullYear())) }

ECMAScript国际化API将month转换为本地语言(例如:october 11)

const convertDate = new Date('10/11/2009') Const lang = 'fr' // de, es, ch if (convertDate !== '无效日期' && !isNaN(convertDate)) { console.log(convertDate. getdate () + ' ' + convertDate. log)toLocaleString(朗,{ 月:“长” })) }