ArkTS对时间的处理?

如题:ArkTS对时间的处理?

阅读 2.1k
1 个回答

功能场景描述及使用场景

本文主要介绍如何获取系统当前时间或者指定时间,已经对时间格式进行转换。本文档参考@ohos.systemDateTime (系统时间、时区)-ArkTS API参考-HarmonyOS应用开发。主要使用@ohos.systemDateTime 对时间进行处理,以及使用第三方库对时间格式进行转换

使用 @ohos.systemDateTime

导入模块

import systemDateTime from '@ohos.systemDateTime';

使用getCurrentTime(isNano: boolean, callback: AsyncCallback<number\>): void

获取自Unix纪元以来经过的时间,使用callback异步回调。

系统能力: SystemCapability.MiscServices.Time

参数:

<p id="p238175645812">参数名</p> <p id="p23811156205815">类型</p> <p id="p638110565589">必填</p> <p id="p33812561581">说明</p>
<p id="p15381135618588">isNano</p> <p id="p1438145612589">boolean</p> <p id="p338165620581">是</p> <p id="p63811756165811">返回结果是否为纳秒数。- true:表示返回结果为纳秒数(ns)。- false:表示返回结果为毫秒数(ms)。</p>
<p id="p113814566583">callback</p> <p id="p113821956185815">AsyncCallback<number></p> <p id="p43821560583">是</p> <p id="p2382165695815">回调函数,返回自Unix纪元以来经过的时间戳。</p>
try {
 systemDateTime.getCurrentTime(true, (error: BusinessError, time: number) => {
   if (error) {
     console.info(`Failed to get currentTime. message: ${error.message}, code: ${error.code}`);
     return;
  }
   console.info(`Succeeded in getting currentTime : ${time}`);
});
} catch(e) {
 let error = e as BusinessError;
 console.info(`Failed to get currentTime. message: ${error.message}, code: ${error.code}`);
}

其他的获取时间能力请参考API文档,下面是目前支持的API

使用Date

systemDateTime.getDate从API9支持,从API10开始废弃,使用new Date()代替

使用方式如下所示

let date: Date = new Date();

常用API

console.info(`date.toString() res : ` + date.toString());
console.info(`date.toDateString() res: ` + date.toDateString());
console.info(`date.toTimeString() res: ` + date.toTimeString());
console.info(`date.toLocaleString() res: ` + date.toLocaleString());
console.info(`date.toLocaleDateString() res: ` + date.toLocaleDateString());
console.info(`date.getFullYear() res: ` + date.getFullYear());
console.info(`date.getUTCFullYear() res: ` + date.getUTCFullYear());
console.info(`date.getMonth() res: ` + date.getMonth());
console.info(`date.getUTCDay() res: ` + date.getUTCDay());
console.info(`date.getDay() res: ` + date.getDay());

输出结果如下所示

date.toDateString() res: Wed Nov 15 2023
date.toTimeString() res: 17:40:43 GMT+0800
date.toLocaleString() res: 11/15/2023, 5:40:43 PM
date.toLocaleDateString() res: 11/15/2023
date.getFullYear() res: 2023
date.getUTCFullYear() res: 2023
date.getMonth() res: 10
date.getUTCDay() res: 3
date.getDay() res: 3

更多支持的API,参考lib.ets.5.ts

使用第三方库

如果上面的API无法满足要求,可以使用第三方库,本文主要使用dayjs,其他的第三方库可以自行根据官方文档使用,dayjs链接OpenHarmony-TPC/openharmony\_tpc\_samples - Gitee.com

下载

oh-package.json5配置文件中添加dayjs的依赖

"dayjs": "1.11.7"

添加依赖的格式如下所示

"dependencies": {
​
 "@ohos/fooLib": "file:xxx/fooLib", // 本地引用
​
 "remoteLib1": "1.0.0" // 远程引用,固定版本
​
 "remoteLib2": "~3.2.1" // 远程引用,保持主版本和次版本不变,patch 版本可更新到最新版本。即 >=3.2.1, <3.3.0
​
 "remoteLib3": "^3.1.0" // 远程引用,保持主版本不变,次版本、patch 版本可更新到最新版本。即 >=3.1.0, <4.0.0
​
}

使用

import dayjs from "dayjs";
​
dayjs("2018-08-08"); // 解析
​
dayjs().format("{YYYY} MM-DDTHH:mm:ss SSS [Z] A"); // 展示
dayjs(date).format("YYYY-MM-DD HH:mm:ss") // 展示
​
dayjs().set("month", 3).month(); // 获取
​
dayjs().add(1, "year"); // 处理
​
dayjs().isBefore(dayjs()); // 查询

使用插件

import weekOfYear from "dayjs/plugin/weekOfYear"; //导入该插件
 
 
console.info(`DayOfWeek is: ` + dayjs(date).format("d"));
console.info(`DayOfMonth is: ` + dayjs(date).format("DD"));
 
dayjs.extend(weekOfYear) //使用该插件
console.info(`WeekOfYear is: ` + dayjs(date).week());

更多关于dayjs的使用请参考Day.js中文网 (fenxianglu.cn)

更多支持的第三方库请参考README.md · OpenHarmony-TPC/openharmony\_tpc\_samples - Gitee.com

适配的版本信息

  • IDE:DevEco Studio 4.0.1.601
  • SDK:HarmoneyOS 4.0.10.11
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进