如何从JavaScript日期获取月份名称

本文介绍了一种在JavaScript中从Date对象获取月份名称的方法。通过使用Date对象的toLocaleString()方法,可以轻松地以长格式或短格式获取当前语言环境下的月份名称。此外,还可以指定特定的语言环境来获取相应的月份名称。

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/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值