Use toLocaleDateString()
(使用toLocaleDateString()
)
The toLocaleDateString()
method returns a string with a language-sensitive representation of the date portion of the date.
(toLocaleDateString()
方法返回一个字符串,其中包含日期的日期部分的语言敏感表示。)
The locales and options arguments let applications specify the language whose formatting conventions should be used and allow to customize the behavior of the function.(语言环境和选项参数使应用程序可以指定应使用其格式约定的语言,并允许自定义函数的行为。)
The values you can passed in options for different keys:(您可以在不同键的选项中传递的值:)
- day:
(天:)
The representation of the day.(一天的表示形式。)
Possible values are "numeric", "2-digit".(可能的值为“数字”,“ 2位”。)
- weekday:
(工作日:)
The representation of the weekday.(工作日的表示形式。)
Possible values are "narrow", "short", "long".(可能的值为“ narrow”,“ short”,“ long”。)
- year:
(年:)
The representation of the year.(年份的表示形式。)
Possible values are "numeric", "2-digit".(可能的值为“数字”,“ 2位”。)
- month:
(月:)
The representation of the month.(月份的表示形式。)
Possible values are "numeric", "2-digit", "narrow", "short", "long".(可能的值为“数字”,“两位数”,“窄”,“短”,“长”。)
- hour:
(小时:)
The representation of the hour.(小时的表示形式。)
Possible values are "numeric", "2-digit".(可能的值为“数字”,“ 2位”。)
- minute: The representation of the minute.
(分钟:分钟的表示形式。)
Possible values are "numeric", "2-digit".(可能的值为“数字”,“ 2位”。)
- second:
(第二:)
The representation of the second.(第二个的表示形式。)
Possible values are "numeric", 2-digit".(可能的值为“数字”,“ 2位数字”。)
All these keys are optional.
(所有这些键都是可选的。)
You can change the number of options values based on your requirements, and this will also reflect the presence of each date time term.(您可以根据需要更改选项值的数量,这也将反映每个日期时间项的存在。)
Note: If you would only like to configure the content options, but still use the current locale, passing null
for the first parameter will cause an error.
(注意:如果只想配置内容选项,但仍使用当前语言环境,则为第一个参数传递null
会导致错误。)
Use undefined
instead.(请改用undefined
。)
For different languages:(对于不同的语言:)
- "en-US": For English
(“ en-US”:英语)
- "hi-IN": For Hindi
(“ hi-IN”:用于印地语)
- "ja-JP": For Japanese
(“ ja-JP”:对于日语)
You can use more language options.
(您可以使用更多语言选项。)
For example(例如)
var options = { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' }; var today = new Date(); console.log(today.toLocaleDateString("en-US")); // 9/17/2016 console.log(today.toLocaleDateString("en-US", options)); // Saturday, September 17, 2016 console.log(today.toLocaleDateString("hi-IN", options)); // ??????, 17 ?????? 2016
You can also use the toLocaleString()
method for the same purpose.
(您也可以出于相同目的使用toLocaleString()
方法。)
The only difference is this function provides the time when you don't pass any options.(唯一的区别是此功能提供了您不传递任何选项的时间。)
// Example
9/17/2016, 1:21:34 PM
References:(参考文献:)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…