Given a JavaScript Date object instance, how can you get a string that represents the month name?
给定一个JavaScript Date对象实例,如何获得代表月份名称的字符串?
In other words, from
换句话说,从
const today = new Date()
how can we get the month name?
我们如何获得月份名称 ?
Every Date object instance has a toLocaleString()
method, which is one of the JavaScript internationalization methods.
每个Date对象实例都有一个toLocaleString()
方法,这是JavaScript国际化方法之一 。
Using it you can get the month name in your current locale, and here’s how you can use it:
使用它,您可以在当前语言环境中获得月份名称,这是使用方法:
const today = new Date()
today.toLocaleString('default', { month: 'long' })
Depending on your current locale you’ll get a different result. I get “October” as a result.
根据您当前的语言环境,您将获得不同的结果。 结果是“十月”。
Using the short
format for the date, I get “Oct”:
使用日期的short
格式,我得到“ Oct”:
today.toLocaleString('default', { month: 'short' })
The first parameter, to which we pass the default
string, is the locale. You can pass any locale you want, for example it-IT
will return you ottobre
:
我们将default
字符串传递给的第一个参数是语言环境。 您可以传递任何想要的语言环境,例如it-IT
将返回ottobre
:
const today = new Date()
today.toLocaleString('it-IT', { month: 'long' })
翻译自: https://flaviocopes.com/how-to-get-month-from-javascript-date/