date.toLocaleTimeString
The toLocaleTimeString()
方法返回该日期对象时间部分的字符串,该字符串格式因不同语言而不同。新增的参数 locales
和 options
使程序能够指定使用哪种语言格式化规则,允许定制该方法的表现(behavior)。在旧版本浏览器中, locales
和 options
参数被忽略,使用的语言环境和返回的字符串格式是各自独立实现的。
语法
dateObj.toLocaleTimeString([locales[, options]])
参数
查看浏览器兼容性小节,看下哪些浏览器支持 locales
和 options
参数,还可以参看例子:检测 locales
和 options
参数支持情况。
locales
可选的。一个带有BCP 47语言标签的字符串或这种字符串的数组。有关参数的一般形式和解释locales
,请参阅Intl页面。以下Unicode扩展键是被允许的:
nu
编号系统。可能的值包括:
"arab"
,"arabext"
,"bali"
,"beng"
,"deva"
,"fullwide"
,"gujr"
,"guru"
,"hanidec"
,"khmr"
,"knda"
,"laoo"
,"latn"
,"limb"
,"mlym"
,"mong"
,"mymr"
,"orya"
,"tamldec"
,"telu"
,"thai"
,"tibt"
。
ca
日历。可能的值包括:
"buddhist"
,"chinese"
,"coptic"
,"ethioaa"
,"ethiopic"
,"gregory"
,"hebrew"
,"indian"
,"islamic"
,"islamicc"
,"iso8601"
,"japanese"
,"persian"
,"roc"
。
options
可选的。具有部分或全部以下属性的对象:
localeMatcher
要使用的语言环境匹配算法。可能的值是"lookup"
和"best fit"
; 默认是"best fit"
。有关此选项的信息,请参阅Intl页面。
timeZone
要使用的时区。唯一的价值实现必须认识到的是"UTC"
; 默认是运行时的默认时区。实现也可以识别的时区名称IANA时区数据库,如"Asia/Shanghai"
,"Asia/Kolkata"
,"America/New_York"
。
hour12
是否使用12小时的时间(而不是24小时的时间)。可能的值是true
和false
; 默认值是语言环境相关的。
formatMatcher
要使用的格式匹配算法。可能的值是"basic"
和"best fit"
; 默认是"best fit"
。有关使用此属性的信息,请参阅以下段落。
以下属性描述了格式化输出中使用的日期时间组件以及它们所需的表示。实现需要至少支持以下子集:
weekday
,year
,month
,day
,hour
,minute
,second
weekday
,year
,month
,day
year
,month
,day
year
,month
month
,day
hour
,minute
,second
hour
,minute
实现可以支持其他子集,并且将针对所有可用的子集表示组合来协商请求以找到最佳匹配。两种算法可用于此协商并由formatMatcher
属性选择:完全指定的"basic"
算法和依赖于实现的"best fit"
算法。
weekday
周日的表示。可能的值是"narrow"
,"short"
,"long"
。
era
时代的代表。可能的值是"narrow"
,"short"
,"long"
。
year
年的表示。可能的值是"numeric"
,"2-digit"
。
month
月份的表示。可能的值是"numeric"
,"2-digit"
,"narrow"
,"short"
,"long"
。
day
一天的表示。可能的值是"numeric"
,"2-digit"
。
hour
小时的表示。可能的值是"numeric"
,"2-digit"
。
minute
分钟的表示。可能的值是"numeric"
,"2-digit"
。
second
第二个的表示。可能的值是"numeric"
,"2-digit"
。
timeZoneName
时区名称的表示。可能的值是"short"
,"long"
。
每个日期时组件属性的默认值是undefined
,但如果hour
,minute
,second
性质都是undefined
,然后hour
,minute
以及second
被假定为是"numeric"
。
返回值
Date
根据特定于语言的约定表示给定实例的时间部分的字符串。
例子
Using toLocaleTimeString()
没有指定语言环境(locale)时,返回一个使用默认语言环境和格式设置(options)的格式化字符串。
var date = new Date(Date.UTC(2012, 11, 12, 3, 0, 0));
// toLocaleTimeString() without arguments depends on the implementation,
// the default locale, and the default time zone
console.log(date.toLocaleTimeString());
// → "7:00:00 PM" if run in en-US locale with time zone America/Los_Angeles
检测 locales
和 options
支持情况
locales
和 options
参数不是所有的浏览器都支持。为了检测一种实现环境(implementation)是否支持它们,可以使用不合法的语言标签,如果实现环境支持该参数,则会抛出一个 RangeError
异常,反之会忽略参数。
function toLocaleTimeStringSupportsLocales() {
try {
new Date().toLocaleTimeString('i');
} catch (e) {
return e.name === 'RangeError';
}
return false;
}
Using locales
下例展示了本地化时间格式的一些变化。为了在应用的用户界面得到某种语言的时间格式,必须确保使用 locales
参数指定了该语言(可能还需要设置某些回退语言)。
var date = new Date(Date.UTC(2012, 11, 20, 3, 0, 0));
// formats below assume the local time zone of the locale;
// America/Los_Angeles for the US
// US English uses 12-hour time with AM/PM
console.log(date.toLocaleTimeString('en-US'));
// → "7:00:00 PM"
// British English uses 24-hour time without AM/PM
console.log(date.toLocaleTimeString('en-GB'));
// → "03:00:00"
// Korean uses 12-hour time with AM/PM
console.log(date.toLocaleTimeString('ko-KR'));
// → "오후 12:00:00"
// Arabic in most Arabic speaking countries uses real Arabic digits
console.log(date.toLocaleTimeString('ar-EG'));
// → "٧:٠٠:٠٠ م"
// when requesting a language that may not be supported, such as
// Balinese, include a fallback language, in this case Indonesian
console.log(date.toLocaleTimeString(['ban', 'id']));
// → "11.00.00"
Using options
可以使用 options
参数来自定义 toLocaleTimeString
方法返回的字符串。
var date = new Date(Date.UTC(2012, 11, 20, 3, 0, 0));
// an application may want to use UTC and make that visible
var options = { timeZone: 'UTC', timeZoneName: 'short' };
console.log(date.toLocaleTimeString('en-US', options));
// → "3:00:00 AM GMT"
// sometimes even the US needs 24-hour time
console.log(date.toLocaleTimeString('en-US', { hour12: false }));
// → "19:00:00"
性能
当格式化大量日期时,最好创建一个 Intl.DateTimeFormat
对象,然后使用该对象 format
属性提供的方法。
规格
Specification | Status | Comment |
---|---|---|
ECMAScript 3rd Edition (ECMA-262) | Standard | Initial definition. Implemented in JavaScript 1.0. |
ECMAScript 5.1 (ECMA-262)The definition of 'Date.prototype.toLocaleTimeString' in that specification. | Standard | |
ECMAScript 2015 (6th Edition, ECMA-262)The definition of 'Date.prototype.toLocaleTimeString' in that specification. | Standard | |
ECMAScript Latest Draft (ECMA-262)The definition of 'Date.prototype.toLocaleTimeString' in that specification. | Draft | |
ECMAScript Internationalization API 1.0 (ECMA-402)The definition of 'Date.prototype.toLocaleTimeString' in that specification. | Standard | Defines locales and options arguments. |
ECMAScript Internationalization API 2.0 (ECMA-402)The definition of 'Date.prototype.toLocaleTimeString' in that specification. | Standard | |
ECMAScript Internationalization API 4.0 (ECMA-402)The definition of 'Date.prototype.toLocaleTimeString' in that specification. | Draft | |
浏览器兼容性
Feature | Chrome | Firefox | Edge | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|---|
Basic Support | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
locales | 24 | 29 | ? | 11 | 15 | 10 |
options | 24 | 29 | ? | 11 | 15 | 10 |
IANA time zone names in timeZone option | 24 | 52 | ? | ? | ? | ? |
Feature | Android | Chrome for Android | Edge mobile | Firefox for Android | IE mobile | Opera Android | iOS Safari |
---|---|---|---|---|---|---|---|
Basic Support | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
locales | (No) | 26 | ? | (No) | (No) | (No) | 10 |
options | (No) | 26 | ? | (No) | (No) | (No) | 10 |
IANA time zone names in timeZone option | ? | ? | ? | (No) | ? | ? | ? |
本文档系腾讯云开发者社区成员共同维护,如有问题请联系 cloudcommunity@tencent.com