赞
踩
列表渲染 / Intl接口 / 国际化
音频播放应用在4.1.3.5 版本x5设备和204.1.0.33版本NOH-AN00设备上,列表渲染异常,hilog日志报错信息显示Intl接口发生错误。
具体hilog日志信息如下:
- 12-28 20:27:53.547 26915-26915 A03D00/JSAPP com.yt.radioHarmony E Error message:Invalid option : option
- 12-28 20:27:53.547 26915-26915 A03D00/JSAPP com.yt.radioHarmony E SourceCode:
- 12-28 20:27:53.547 26915-26915 A03D00/JSAPP com.yt.radioHarmony E let formatSdf = new Intl.DateTimeFormat("zh-CN", {
- 12-28 20:27:53.547 26915-26915 A03D00/JSAPP com.yt.radioHarmony E ^
- 12-28 20:27:53.547 26915-26915 A03D00/JSAPP com.yt.radioHarmony E Stacktrace:
- 12-28 20:27:53.547 26915-26915 A03D00/JSAPP com.yt.radioHarmony E at publishTimeToNowDistance (entry/src/main/ets/utils/utils.ts:260:21)
- 12-28 20:27:53.547 26915-26915 A03D00/JSAPP com.yt.radioHarmony E at anonymous (entry/src/main/ets/module/album/AlbumDetailsListComponent.ets:306:16)12-28 20:27:53.547 26915-26915 A03D00/JSAPP com.yt.radioHarmony E at initialRender (entry/src/main/ets/module/album/AlbumDetailsListComponent.ets:305:16)
复制
其中utils.ts文件的publishTimeToNowDistance相关代码如下:
- /**
- * 发布时间格式化
- * */
- export function publishTimeToNowDistance(publishTime: number): string {
- if (publishTime <= 0) {
- return "";
- }
- let formatSdf = new Intl.DateTimeFormat("zh-CN", {
- hour12: false,
- month: '2-digit',
- day: '2-digit',
- minute: 'numeric',
- second: 'numeric',
- dateStyle:'short'
- })
- let formatSdfYear = new Intl.DateTimeFormat("zh-CN", {
- hour12: false,
- year: 'numeric',
- month: '2-digit',
- day: '2-digit',
- minute: 'numeric',
- second: 'numeric',
- dateStyle:'short'
- })
-
- let nowTimeML = Date.now()
- let distance = nowTimeML - publishTime
- if (distance < 10 * 60 * 1000) {
- return '刚刚'
- } else if (distance < 60 * 60 * 1000) {
- return `${Math.floor(distance / (60 * 1000))}分钟前`
- } else if (distance < 24 * 60 * 60 * 1000) {
- return `${Math.floor(distance / (60 * 60 * 1000))}小时前`
- } else if (distance < 48 * 60 * 60 * 1000) {
- return '1天前'
- } else {
- let publishCalendar = new Date(publishTime)
- let yearPublish = publishCalendar.getFullYear()
-
- let nowCalendar = new Date()
- let yearNow = nowCalendar.getFullYear()
- if (yearPublish === yearNow) {
- return formatSdf.format(publishTime).replace(/\//g ,'-')
- } else {
- return formatSdfYear.format(publishTime).replace(/\//g ,'-')
- }
- }
- }
复制
使用@ohos.intl (国际化-Intl)接口,代码的引用头文件需要import Intl from '@ohos.intl'。
代码也需要修改一下,请参考这个文档修改。
文档地址:文档中心
其中:dateStyle这个参数会和年月日时分秒的单独设置冲突,建议去掉。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。