在JavaScript中,我该如何从这个日期对象生成月份的名称(例如:Oct/October)?
var objDate = new Date("10/11/2009");
在JavaScript中,我该如何从这个日期对象生成月份的名称(例如:Oct/October)?
var objDate = new Date("10/11/2009");
我的最佳解决方案如下:
var dateValue = Date();
var month = dateValue.substring(4,7);
var date = dateValue.substring(8,10);
var year = dateValue.substring(20,24);
var finaldateString = date+"-"+month+"-"+year;
$.datepicker.setDefaults( $.datepicker.regional[ "nl" ] ); // dutch
$.datepicker.formatDate( "dd MM yy", objDate );
将名称存储在数组中,并通过月份的索引进行查找。
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()]);
var month = [ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ];
呢?这比逐个添加更短。 - Fizzix.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;
}
};
}
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.monthNames = function( ) {
var arrMonth = [],
dateRef = new Date(),
year = dateRef.getFullYear();
dateRef.setMonth(0);
while (year == dateRef.getFullYear()) {
/* push le mois en lettre et passe au mois suivant */
arrMonth.push( (dateRef.toLocaleString().split(' '))[2] );
dateRef.setMonth( dateRef.getMonth() + 1);
}
return arrMonth;
}
alert(Date.monthNames().toString());
// -> janvier,février,mars,avril,mai,juin,juillet,août,septembre,octobre,novembre,décembre
const monthNumber = 8;
const yearNumber = 2018;
const date = `${['Jan', 'Feb', 'Mar', 'Apr',
'May', 'Jun', 'Jul', 'Aug',
'Sep', 'Oct', 'Nov', 'Dec'][monthNumber - 1]
} ${yearNumber}`;
console.log(date);
toLocaleString
:
function 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));
在众多其他出色的答案基础上进行扩展 - 如果您正在使用jQuery - 您可以尝试以下代码:
$.fn.getMonthName = function(date) {
var monthNames = [
"January", "February", "March",
"April", "May", "June",
"July", "August", "September",
"October", "November", "December"
];
return monthNames[date.getMonth()];
};
其中date
等于var d = new Date(somevalue)
。这样做的主要优点是避免全局命名空间,正如@nickf所说。