如何在JavaScript中从这个日期对象生成月份的名称(例如:10月/ 10月)?
var objDate = new Date("10/11/2009");
如何在JavaScript中从这个日期对象生成月份的名称(例如:10月/ 10月)?
var objDate = new Date("10/11/2009");
当前回答
这里是一个函数,你将1传递给12,并返回完整的月份名称例如" January "或" July "
function getMonthName(monthNumber) {
return new Date('1999-' + monthNumber + '-15').toLocaleString('en-us', { month: 'long' })
}
其他回答
在IE 11, Chrome, Firefox上测试
const dt = new Date(); Const locale = navigator。语言!= undefined ?导航器。[0]: navigator.language; const fullMonth = dt。toLocaleDateString(区域设置,{month: 'long'}); console.log (fullMonth);
我用了一个很有效的小窍门:
const monthNumber = 8; const yearNumber = 2018; const date = ' ${['Jan', 'Feb', 'Mar', 'Apr', “五月”,“六月”,“七月”,“八月”, 'Sep', 'Oct', 'Nov', 'Dec'][monthNumber - 1] } $ {yearNumber} '; console.log(日期);
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
对于momentjs,只需使用格式符号。
const myDate = new Date()
const shortMonthName = moment(myDate).format('MMM') // Aug
const fullMonthName = moment(myDate).format('MMMM') // August
这里是一个函数,你将1传递给12,并返回完整的月份名称例如" January "或" July "
function getMonthName(monthNumber) {
return new Date('1999-' + monthNumber + '-15').toLocaleString('en-us', { month: 'long' })
}