如何在JavaScript中从这个日期对象生成月份的名称(例如:10月/ 10月)?
var objDate = new Date("10/11/2009");
如何在JavaScript中从这个日期对象生成月份的名称(例如:10月/ 10月)?
var objDate = new Date("10/11/2009");
当前回答
我想到了一个部分解决方案。它使用正则表达式提取月份和日期名称。但当我查看区域和语言选项(窗口)时,我意识到不同的文化有不同的格式顺序……也许更好的正则表达式模式会有用。
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原型(并且有一些很好的理由不想这样做),您实际上可以提出一个非常简单的方法:
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对象。
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
如果你不想使用一个外部库,或者存储一个月份名称的数组,或者如果ECMAScript国际化API因为浏览器兼容性而不够好,你可以通过从日期输出中提取信息来实现:
var now = new Date();
var monthAbbrvName = now.toDateString().substring(4, 7);
这将为您提供缩写的月份名称,例如october。我相信日期将以各种格式出现,这取决于初始化和您的地区,因此请查看toDateString()返回的内容,并基于此重新计算您的substring()值。
另一种格式化日期的方法
new Date().toLocaleString('en-us',{month:'long', year:'numeric', day:'numeric'}) //output: "May 21, 2019"
使用JavaScript将Date格式化为“dd- mm -yyyy”,请使用下面的代码
const monthNames = [“一月”、“二月”、“三月”、“四月”、“五月”、“六月”、“七月”、“八月”、“九月”、“十月”、“十一月”、“十二月” ]; 常量 d = 新日期(); var dd = String(d.getDate())).padStart(2, '0'); var mm = String(d.getMonth() + 1).padStart(2, '0'); var yyyy = d.getFullYear(); var fullDate = +dd +“-”+ monthNames[d.getMonth()] +“-”+ yyyy; document.write(“日期是:”+ 完整日期);