如何在JavaScript中从这个日期对象生成月份的名称(例如:10月/ 10月)?
var objDate = new Date("10/11/2009");
如何在JavaScript中从这个日期对象生成月份的名称(例如:10月/ 10月)?
var objDate = new Date("10/11/2009");
当前回答
现在的自然格式是使用Moment.js。
在Moment.js中,以字符串格式获取月份的方法非常简单,不需要在代码中硬编码月份名称: 要获得当前月份和年份的月份名称格式和全年(2015年5月):
moment(new Date).format("MMMM YYYY");
其他回答
将名称存储在一个数组中,并根据月份的索引进行查找。
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()方法
较短的版本:
const monthNames = ["January", "February", "March", "April", "May", "June", “七月”,“八月”,“九月”,“十月”,“十一月”,“十二月” ]; const d = new Date(); 文档。write("当前月份是" + monthNames[d.getMonth()]);
注(2019-03-08)-我最初在2009年写的这个答案已经过时了。请参阅David Storey的回答以获得更好的解决方案。
我想到了一个部分解决方案。它使用正则表达式提取月份和日期名称。但当我查看区域和语言选项(窗口)时,我意识到不同的文化有不同的格式顺序……也许更好的正则表达式模式会有用。
function testDateInfo() {
var months = new Array();
var days = new Array();
var workingDate = new Date();
workingDate.setHours(0, 0, 0, 0);
workingDate.setDate(1);
var RE = new RegExp("([a-z]+)","ig");
//-- get day names 0-6
for (var i = 0; i < 7; i++) {
var day = workingDate.getDay();
//-- will eventually be in order
if (days[day] == undefined)
days[day] = workingDate.toLocaleDateString().match(RE)[0];
workingDate.setDate(workingDate.getDate() + 1);
}
//--get month names 0-11
for (var i = 0; i < 12; i++) {
workingDate.setMonth(i);
months.push(workingDate.toLocaleDateString().match(RE)[1]);
}
alert(days.join(",") + " \n\r " + months.join(","));
}
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
只需要编写一个简单的toLocaleString包装器:
函数 LocalDate(locale) { this.locale = locale; } LocalDate.prototype.getMonthName = function(date) { return date.toLocaleString(this.locale,{month:“long”}); }; var objDate = new Date(“10/11/2009”); var localDate = new LocalDate(“en”); console.log(localDate.getMonthName(objDate)); localdate.locale = “ru”; console.log(localDate.getMonthName(objDate)); localdate.locale = “zh”; console.log(localDate.getMonthName(objDate));