如何在JavaScript中从这个日期对象生成月份的名称(例如:10月/ 10月)?
var objDate = new Date("10/11/2009");
如何在JavaScript中从这个日期对象生成月份的名称(例如:10月/ 10月)?
var objDate = new Date("10/11/2009");
当前回答
你可以使用或不使用当地语言翻译
创造价值“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(朗,{ 月:“长” })) }
其他回答
文档。写(新日期()。toLocaleString('en-us',{月:'长',年:'数字',日:'数字'}))
对于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' })
}
您可以简单地使用date . tolocaledatestring()并解析所需的日期作为参数
const事件=新的日期(日期。Utc (2012, 11,20,3,0,0)); Const选项={年:'numeric',月:'short',日:'numeric'}; console.log(事件。toLocaleDateString(“de-DE”,选项)); //期望输出:Donnerstag, 20。Dezember 2012 console.log(事件。toLocaleDateString(“en - us”,选项)); //美国格式 //如果你只想要这个月 console.log(事件。toLocaleDateString(undefined, {month: 'short'})); console.log(事件。toLocaleDateString(undefined, {month: 'long'}));
您可以在Firefox文档中找到更多信息
如果你使用剑道,也可以这样做。
kendo.toString(dateobject, "MMMM");
以下是kendo网站上的格式化器列表:
"d" Renders the day of the month, from 1 through 31. "dd" The day of the month, from 01 through 31. "ddd" The abbreviated name of the day of the week. "dddd" The full name of the day of the week. "f" The tenths of a second in a date and time value. "ff" The hundredths of a second in a date and time value. "fff" The milliseconds in a date and time value. "M" The month, from 1 through 12. "MM" The month, from 01 through 12. "MMM" The abbreviated name of the month. "MMMM" The full name of the month. "h" The hour, using a 12-hour clock from 1 to 12. "hh" The hour, using a 12-hour clock from 01 to 12. "H" The hour, using a 24-hour clock from 1 to 23. "HH" The hour, using a 24-hour clock from 01 to 23. "m" The minute, from 0 through 59. "mm" The minute, from 00 through 59. "s" The second, from 0 through 59. "ss" The second, from 00 through 59. "tt" The AM/PM designator. "yy" The last two characters from the year value. "yyyy" The year full value. "zzz" The local timezone when using formats to parse UTC date strings.