1# 时间日期国际化 2 3## 使用场景 4 5在不同的国家和文化中,时间和日期格式的表示方法有所不同,使用惯例的不同点包括:日期中年月日的顺序、时间中时分秒的分隔符等。若应用中需展示时间日期,要确保界面以合适的方式显示,以便用户能够理解。 6 7时间日期国际化包括时间日期格式化、相对时间格式化、时间段格式化。时间日期格式化是指将时间和日期转换为指定格式的字符串。相对时间格式化是指将一个时间点与另一个时间点之间的时间差转换为指定格式,时间差如“30秒前”、“1天后”。时间段格式化是指将一段时间转换为指定格式,时间段如“星期三”、“8:00--11:30”。 8 9## 约束与限制 10 111. 日期格式和时间格式需同时设置。若设置了时间格式,未设置日期格式,只显示时间格式;若设置了日期格式,未设置时间格式,只显示日期格式。 12 132. 若设置了时间或日期格式,则不支持设置年、月、日、时、分、秒、工作日格式;不设置时间或日期格式时,支持独立设置年、月、日、时、分、秒、工作日格式。 14 15## 开发步骤 16 17### 时间日期和相对时间格式化 18 19时间日期格式化将表示时间日期的Date对象,通过[DateTimeFormat](../reference/apis-localization-kit/js-apis-intl.md#datetimeformat)类的[format](../reference/apis-localization-kit/js-apis-intl.md#format)接口实现格式化,具体开发步骤如下。 20 211. 导入模块。 22 ```ts 23 import { intl } from '@kit.LocalizationKit'; 24 ``` 25 262. 创建DateTimeFormat对象。 27 传入单独的locale参数或locale列表,若传入列表使用第一个有效的locale创建对象。不传入locale参数时,使用系统当前的locale创建对象。 28 构造函数支持通过DateTimeOptions设置不同的时间日期格式,具体请参考表1-表10。 29 30 ```ts 31 let dateFormat: intl.DateTimeFormat = new intl.DateTimeFormat(locale: string | Array<string>, options?: DateTimeOptions); 32 let dateFormat: intl.DateTimeFormat = new intl.DateTimeFormat(); //不传入locale参数 33 ``` 34 353. 时间日期和相对时间格式化。 36 ```ts 37 // 时间日期格式化 38 let formattedDate: string = dateFormat.format(date: Date); 39 40 // 相对时间格式化 41 let formattedDateRange: string = dateFormat.formatRange(startDate: Date, endDate: Date); 42 ``` 43 444. 获取格式化选项,查看对象的设置信息。 45 ```ts 46 let options: intl.DateTimeOptions = dateFormat.resolvedOptions(); 47 ``` 48 49**时间日期格式化选项** 50 51以时间:2021年9月17日 13:04:00、2021年9月17日 00:25:00,locale: zh-CN和en为例,说明[DateTimeOptions](../reference/apis-localization-kit/js-apis-intl.md#datetimeoptions)不同的取值和显示结果。 52 53**表1** 日期显示格式(dateStyle) 54 55| 取值 | 描述 | 2021年9月17日 13:04:00,locale为zh-CN显示结果 | 2021年9月17日 13:04:00,locale为en显示结果 | 56| ------ | --------------------------------------- | ------------------------------------------ | ---------------------------------------- | 57| full | 完整的日期显示,包含年份、月份、天数和星期。| 2021年9月17日星期五 | Friday, September 17, 2021 | 58| long | 详细的日期显示,包含年份、月份和天数。 | 2021年9月17日 | September 17, 2021 | 59| short | 简短的日期显示,包含年份、月份和天数。 | 2021/9/17 | 9/17/21 | 60| medium | 中等长度日期显示,包含年份、月份和天数。 | 2021年9月17日 | Sep 17, 2021 | 61 62**表2** 时间显示格式(timeStyle) 63 64| 取值 | 描述 | 2021年9月17日 13:04:00,locale为zh-CN显示结果 | 2021年9月17日 13:04:00,locale为en显示结果 | 65| ------ | ------------- | -------- | -------- | 66| full | 完整的时间显示,包含时区和时间,时间精确到秒。 | 中国标准时间 13:04:00 | 13:04:00 China Standard Time | 67| long | 详细的时间显示,包含时区和时间,时区以GMT+时区偏移表示,时间精确到秒。 | GMT+8 13:04:00 | 13:04:00 GMT+8 | 68| short | 简短时间显示,包含小时和分钟。 | 13:04 | 13:04 | 69| medium | 中等长度时间显示,包含小时、分钟和秒。 | 13:04:00 | 13:04:00 | 70 71**表3** 年份显示格式(year) 72 73| 取值 | 描述 | 2021年9月17日 13:04:00,locale为zh-CN显示结果 | 2021年9月17日 13:04:00,locale为en显示结果 | 74| -------- | --------- | -------- | -------- | 75| numeric | 完整的年份显示。 | 2021年 | 2021 | 76| 2-digit | 用完整年份的后2位数字表示年份。 | 21年 | 21 | 77 78**表4** 星期显示格式(weekday) 79 80| 取值 | 描述 | 2021年9月17日 13:04:00,locale为zh-CN显示结果 | 2021年9月17日 13:04:00,locale为en显示结果 | 81| -------- | ------- | -------- | -------- | 82| long | 详细的星期显示。 | 星期五 | Friday | 83| short | 简短的星期显示。 | 周五 | Fri | 84| narrow | 最简短的星期显示。 | 五 | F | 85 86**表5** 时制格式(hourCycle) 87 88| 取值 | 描述 | 2021年9月17日 13:04:00,locale为zh-CN显示结果 | 2021年9月17日 00:25:00,locale为zh-CN显示结果 | 89| --- | --------------- | -------------------------------------------- | ------------------------------------------- | 90| h11 | 用0-11表示小时。 | 下午1:04 | 上午0:25 | 91| h12 | 用1-12表示小时。 | 下午1:04 | 上午12:25 | 92| h23 | 用0-23表示小时。 | 13:04 | 00:25 | 93| h24 | 用1-24表示小时。 | 13:04 | 24:25 | 94 95> **说明** 96> 97> 在不设置dateStyle或timeStyle参数时,hourCycle不同取值的显示效果如上表格。 98 99 100**表6** 时制格式(hourCycle) 101 102| 取值 | 描述 | 2021年9月17日 13:04:00,locale为zh-CN显示结果 | 2021年9月17日 00:25:00,locale为zh-CN显示结果 | 103| --- | --------------- | -------------------------------------------- | ------------------------------------------- | 104| h11 | 用1-24表示小时。 | 下午13:04 | 上午24:25 | 105| h12 | 用1-12表示小时。 | 下午1:04 | 上午12:25 | 106| h23 | 用0-11表示小时。 | 1:04 | 0:25 | 107| h24 | 用0-23表示小时。 | 13:04 | 0:25 | 108 109> **说明** 110> 111> 在设置dateStyle或timeStyle参数时,hourCycle不同取值的显示效果如上表格。 112 113**表7** 月份格式(month) 114 115| 取值 | 描述 | 2021年9月17日 13:04:00,locale为zh-CN显示结果 | 2021年9月17日 13:04:00,locale为en显示结果 | 116| -------- | --------- | -------- | -------- | 117| numeric | 以数字形式显示月份。 | 9月 | 9 | 118| 2-digit | 以两位数字形式显示月份。 | 09月 | 09 | 119| long | 详细的月份显示。 | 九月 | September | 120| short | 简短的月份显示。 | 9月 | Sep | 121| narrow | 最简短的月份显示。 | 9 | S | 122 123**表8** 时区名称的本地化表示(timeZoneName) 124 125| 取值 | 描述 | 2021年9月17日 13:04:00,locale为zh-CN显示结果 | 2021年9月17日 13:04:00,locale为en显示结果 | 126| ----- | ------------------ | -------------------------------------------- | ---------------------------------------- | 127| long | 详细的时区名称显示。 | 中国标准时间 | China Standard Time | 128| short | 简短的时区名称显示。 | GMT+8 | GMT+8 | 129 130**表9** 纪元的显示格式(era) 131 132| 取值 | 描述 | 2021年9月17日 13:04:00,locale为zh-CN显示效果 | 2021年9月17日 13:04:00,locale为en显示效果 | 133| -------- | ------ | -------- | -------- | 134| long | 详细的纪元显示。 | 公元 | Anno Domini | 135| short | 简短的纪元显示。 | 公元 | AD | 136| narrow | 最简短的纪元显示。 | 公元 | A | 137 138**表10** 时段的显示格式(dayPeriod) 139 140| 取值 | 描述 | 2021年9月17日 13:04:00,locale为zh-CN显示效果 | 2021年9月17日 13:04:00,locale为en显示效果 | 141| -------- | ------ | -------- | -------- | 142| long | 详细的时段表述。 | 下午 | in the afternoon | 143| short | 简短的时段表示。 | 下午 | in the afternoon | 144| narrow | 最简短的时段表示。 | 下午 | in the afternoon | 145 146**开发实例** 147```ts 148// 导入模块 149import { intl } from '@kit.LocalizationKit'; 150 151let date = new Date(2021, 8, 17, 13, 4, 0); // 时间日期为2021.09.17 13:04:00 152let startDate = new Date(2021, 8, 17, 13, 4, 0); 153let endDate = new Date(2021, 8, 18, 13, 4, 0); 154 155// 在软件上展示完整的时间信息 156let dateFormat1 = new intl.DateTimeFormat('zh-CN', {dateStyle: 'full', timeStyle: 'full'}); 157let formattedDate1 = dateFormat1.format(date); // formattedDate1: 2021年9月17日星期五 中国标准时间 13:04:00 158 159// 在有限的空间展示简短的时间信息 160let dateFormat2 = new intl.DateTimeFormat('zh-CN', {dateStyle: 'short', timeStyle: 'short'}); 161let formattedDate2 = dateFormat2.format(date); // formattedDate2: 2021/9/17 13:04 162 163// 自定义年月日时分秒的显示效果 164let dateFormat3 = new intl.DateTimeFormat('zh-CN', {year: 'numeric', month: '2-digit', day: '2-digit', hour: '2-digit', minute: '2-digit', second: '2-digit'}); 165let formattedDate3 = dateFormat3.format(date); // formattedDate3: 2021/09/17 13:04:00 166 167// 仅显示一部分时间 168let dateFormat4 = new intl.DateTimeFormat('zh-CN', {month: 'long', day: 'numeric', weekday: 'long' }); 169let formattedDate4 = dateFormat4.format(date); // formattedDate4: 9月17日星期五 170 171// 自定义时制格式 172let dateFormat5 = new intl.DateTimeFormat('zh-CN', {dateStyle: 'short', timeStyle: 'short', hourCycle: 'h11'}); 173let formattedDate5 = dateFormat5.format(date); // formattedDate5: 2021/9/17 下午13:04 174 175// 面向习惯于其他数字系统的用户 176let dateFormat6 = new intl.DateTimeFormat('zh-CN', {dateStyle: 'short', timeStyle: 'short', numberingSystem: 'arab'}); 177let formattedDate6 = dateFormat6.format(date); // formattedDate6: ٢٠٢١/٩/١٧ ١٣:٠٤ 178 179// 格式化时间段 180let dataFormat7 = new intl.DateTimeFormat('en-GB'); 181let formattedDateRange = dataFormat7.formatRange(startDate, endDate); // formattedDateRange: 17/09/2021 - 18/09/2021 182 183// 获取格式化选项 184let dataFormat8 = new intl.DateTimeFormat('en-GB', {dateStyle: 'full'}); 185let options = dataFormat8.resolvedOptions(); 186let dateStyle = options.dateStyle; // dateStyle: full 187``` 188 189### 相对时间格式化 190 191格式化相对时间将表示时间日期的Date对象,通过[RelativeTimeFormat](../reference/apis-localization-kit/js-apis-intl.md#relativetimeformat8)类的[format](../reference/apis-localization-kit/js-apis-intl.md#format8)接口实现格式化,具体开发步骤如下。 192 1931. 导入模块。 194 ```ts 195 import { intl } from '@kit.LocalizationKit'; 196 ``` 197 1982. 创建RelativeTimeFormat对象。 199 构造函数支持通过RelativeTimeFormatInputOptions设置不同的输出消息格式和国际化消息长度,具体请参考表7-表8。 200 ```ts 201 let relativeTimeFormat: intl.RelativeTimeFormat = new intl.RelativeTimeFormat(locale: string | Array<string>, options?: RelativeTimeFormatInputOptions); 202 ``` 203 2043. 格式化相对时间。value为格式化的数值,unit为格式化的单位。 205 ```ts 206 let formattedRelativeTime: string = relativeTimeFormat.format(value: number, unit: string); 207 ``` 208 2094. 自定义相对时间的格式化。 210 ```ts 211 let parts: Array<object> = relativeTimeFormat.formatToParts(value: number, unit: string); 212 ``` 213 2145. 获取相对时间格式化选项,查看对象的设置信息。 215 ```ts 216 let options: intl.RelativeTimeFormatInputOptions = relativeTimeFormat.resolvedOptions(); 217 ``` 218 219**相对时间格式化选项** 220 221以相对时间:一天前,locale: fr-FR和en-GB为例,说明[RelativeTimeFormatInputOptions](../reference/apis-localization-kit/js-apis-intl.md#relativetimeformatinputoptions8)不同的取值和显示结果。 222 223**表11** 数值表示(numeric) 224 225| 取值 | 描述 | 显示效果(fr-FR) | 显示效果(en-GB) | 226| ------ | -------------------------------------------- | -------------- | --------------- | 227| always | 使用数值表示相对时间。 | il y a 1 jour | 1 day ago | 228| auto | 根据locale自适应的选择短语或者数值表示相对时间。 | hier | yesterday | 229 230**表12** 相对时间样式(style) 231 232| 取值 | 描述 | 显示效果(fr-FR) | 显示效果(en-GB) | 233| ------ | -------------------- | -------------- | -------------- | 234| long | 详细的相对时间显示。 | il y a 1 jour | 1 day ago | 235| short | 简短的相对时间显示。 | il y a 1 j | 1 day ago | 236| narrow | 最简短的相对时间显示。 | -1 j | 1 day ago | 237 238 239**开发实例** 240```ts 241// 导入模块 242import { intl } from '@kit.LocalizationKit'; 243 244// 显示相对时间 245let relativeTimeFormat1 = new intl.RelativeTimeFormat('en-GB'); 246let formattedRelativeTime1 = relativeTimeFormat1.format(-1, 'day'); // formattedRelativeTime1: 1 day ago 247 248// 口语化 249let relativeTimeFormat2 = new intl.RelativeTimeFormat('en-GB', {numeric: "auto"}); 250let formattedRelativeTime2 = relativeTimeFormat2.format(-1, 'day'); // formattedRelativeTime2: yesterday 251 252// 部分语言支持更为简短的显示风格 253let relativeTimeFormat3 = new intl.RelativeTimeFormat('fr-FR'); // 默认style为long 254let formattedRelativeTime3 = relativeTimeFormat3.format(-1, 'day'); // formattedRelativeTime3: il y a 1 jour 255let relativeTimeFormat4 = new intl.RelativeTimeFormat('fr-FR', {style: 'narrow'}); 256let formattedRelativeTime4 = relativeTimeFormat4.format(-1, 'day'); // formattedRelativeTime4: -1 j 257 258// 自定义区域设置格式的相对时间格式 259let relativeTimeFormat5 = new intl.RelativeTimeFormat('en-GB', {style: 'long'}); 260// parts: [{type: 'literal', value: 'in'}, {type: 'integer', value: 1, unit: 'day'}, {type: 'literal', value: 'day'}] 261let parts = relativeTimeFormat5.formatToParts(1, 'day'); 262 263// 获取RelativeTimeFormat对象的格式化选项 264let relativeTimeFormat6 = new intl.RelativeTimeFormat('en-GB', {numeric: 'auto'}); 265let options = relativeTimeFormat6.resolvedOptions(); 266let numeric = options.numeric; // numeric: auto 267``` 268