从日期获取月份名称
如何从 JavaScript 中的日期对象生成月份名称(例如:Oct/October)?
var objDate = new Date("10/11/2009");
How can I generate the name of the month (e.g: Oct/October) from this date object in JavaScript?
var objDate = new Date("10/11/2009");
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(30)
现在可以使用 ECMAScript 国际化 API 来做到这一点:
'long'
使用月份的完整名称,'short'
为短名称,'narrow'
为更简单的版本,例如字母语言中的第一个字母。您可以将浏览器的
'default'
区域设置更改为您喜欢的任何区域设置(例如'en-us'
),它将使用该语言/国家/地区的正确名称。使用
toLocaleString
api 您每次都必须传入区域设置和选项。如果您要在多个不同的日期使用相同的区域设置信息和格式选项,则可以使用Intl.DateTimeFormat
代替:有关详细信息,请参阅我关于国际化 API 的博客文章。
It is now possible to do this with the ECMAScript Internationalization API:
'long'
uses the full name of the month,'short'
for the short name, and'narrow'
for a more minimal version, such as the first letter in alphabetical languages.You can change the locale from browser's
'default'
to any that you please (e.g.'en-us'
), and it will use the right name for that language/country.With
toLocaleString
api you have to pass in the locale and options each time. If you are going do use the same locale info and formatting options on multiple different dates, you can useIntl.DateTimeFormat
instead:For more information see my blog post on the Internationalization API.
较短的版本:
注意 (2019-03-08) - 我最初在 2009 年写的这个答案已经过时了。请参阅David Storey 的回答以获得更好的解决方案。
Shorter version:
Note (2019-03-08) - This answer by me which I originally wrote in 2009 is outdated. See David Storey's answer for a better solution.
这是另一种,支持本地化:)
然后您可以轻松添加对其他语言的支持:
Here's another one, with support for localization :)
you can then easily add support for other languages:
如果我们需要传递输入,那么我们需要使用以下方式
input: '2020-12-28'
Code:
Output: " 2020 年 12 月”
If we need to pass our input then we need to use the following way
input: '2020-12-28'
Code:
Output: "Dec 2020"
我衷心推荐来自
format
函数href="http://momentjs.com" rel="noreferrer">moment.js 库,您可以像这样使用它:使用“MMMM”而不是“MMM”,如果你需要月份的全名
除了一长串的其他功能之外,它还具有强大的国际化支持。
I heartily recommend the
format
function from, the moment.js library, which you can use like this:Use "MMMM" instead of "MMM" if you need the full name of the month
In addition to a lengthy list of other features, it has strong support for internationalization.
如果您不介意扩展 Date 原型(并且有一些充分的理由不想这样做),您实际上可以想出一个非常简单的方法:
然后这些函数将应用于所有 javascript 日期对象。
If you don't mind extending the Date prototype (and there are some good reasons to not want to do this), you can actually come up with a very easy method:
These functions will then apply to all javascript Date objects.
如果您赶时间...那就来吧!
预期输出:
“Apr”
If you are in hurry...then here you go!
Expected Output:
"Apr"
您可以简单地使用 Date.toLocaleDateString() 并解析所需的日期作为参数
您可以在 Firefox 文档
You could just simply use Date.toLocaleDateString() and parse the date wanted as parameter
You can find more information in the Firefox documentation
它可以用作
It can be used as
另一种格式化日期的方法
Another way to format date
可以通过此完成日期对象的一些常见的简单过程。
或者你可以制作日期原型,例如
例如:
var dateFormat3 = new Date().getMonthName();
# March
var dateFormat4 = new Date().getFormatDate();
<代码># 2017 年 3 月 16 日Some common easy process from date object can be done by this.
Or you can make date prototype like
Ex:
var dateFormat3 = new Date().getMonthName();
# March
var dateFormat4 = new Date().getFormatDate();
# 16 March, 2017
您可以使用 datejs 来执行此操作。检查 FormatSpecifiers,MMMM 为您提供月份的名称:
并且 datejs 已将其本地化为超过 150 个地点! 查看此处
You might use datejs to do that. Check the FormatSpecifiers, MMMM gives you the month's name:
And datejs got that localized for more than 150 locales! See here
尝试:
Try:
我们也可以将其写成更短的版本,如下所示,而不是声明包含所有月份名称的数组然后用索引指向:
Instead of declaring array which hold all the month name and then pointing with an index, we can also write it in a shorter version as below:
这是一种不依赖于硬编码数组并支持多个区域设置的方法。
如果您需要整个数组:
用法:
如果您只需要选定的月份(更快):
用法:
在 Chrome 和 IE 11 上经过测试并运行良好。在 mozilla 上需要进行一些修改,因为它返回整个日期。
Here's a way that does not depend on a hard-coded array and supports multiple locales.
If you need a whole array:
Usage:
If you need only a selected month (faster):
Usage:
Tested and works fine on Chrome and IE 11. On mozilla some modifications are needed, because it returns the whole date.
最简单、最简单的方法:
The easiest and simplest way:
快速又简单
quick and easy
不幸的是,提取月份名称的最佳方法是从 UTCString 表示形式中提取:
Unfortunately the best way to extract the month name is from the UTCString representation:
在 IE 11、Chrome、Firefox 上测试
Tested on IE 11, Chrome, Firefox
您可以使用多种可用的日期格式化程序之一。由于这属于 JavaScript 规范,因此它将在浏览器和服务器端模式下可用。
例如,
https://developer.mozilla 上有更多内容。 org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date
You can use one of several available Date formatters. Since this falls within the JavaScript specification, it will be available in both browser and server-side modes.
e.g.
There are more at https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date
如今,自然的格式是使用 Moment.js。
在 Moment.js 中以字符串格式获取月份的方法非常简单,无需在代码中硬编码月份名称:
要以月份名称格式和全年(2015 年 5 月)获取当前月份和年份:
The natural format this days is to use Moment.js.
The way to get the month in a string format , is very simple in Moment.js no need to hard code the month names in your code:
To get the current month and year in month name format and full year (May 2015) :
你可以试试这个:
You can try this:
如果您使用剑道,也可以这样做。
以下是来自 kendo 站点 的格式化程序列表:
This can be also done if you are using kendo.
Here are list of formatters from kendo site:
对我来说这是最好的解决方案,
对于 TypeScript 以及
输出来说是
For me this is best solution is,
for TypeScript as well
Output is
您可以处理或不翻译为本地语言
You can handle with or without translating to the local language
也可以按如下方式完成:
It can be done as follows too:
这是一个函数,您传入 1 到 12,然后返回完整的月份名称,例如“January”或“July”
Here's a function where you pass in 1 to 12, and returns back the full month name such as 'January' or 'July'
如果您不想使用外部库,或存储月份名称数组,或者如果 ECMAScript 国际化 API 由于浏览器兼容性而不够好,您始终可以通过从日期输出:
这将为您提供缩写的月份名称,例如 Oct。我相信日期会以各种格式出现,具体取决于初始化和您的区域设置,因此请查看
toDateString()
返回的内容并据此重新计算您的substring()
值。If you don't want to use an external library, or store an array of month names, or if the ECMAScript Internationalization API is not good enough because of browser compatibility you can always do it the old-fashioned way by extracting the info from the date output:
This would give you the abbreviated month name, e.g. Oct. I believe the date will come in all sorts of formats depending on the initialization and your locale so take a look at what
toDateString()
returns and recalculate yoursubstring()
values based on that.要使用 JavaScript 将日期格式设置为“dd-MMM-yyyy”,请使用以下代码
To get Date formatted as "dd-MMM-yyyy" using JavaScript use the below code