当前位置: 首页 > news >正文

鸿蒙开发系统基础能力:【@ohos.systemTime (设置系统时间)】

设置系统时间

本模块用来设置、获取当前系统时间,设置、获取当前系统日期和设置、获取当前系统时区。

icon-note.gif 说明: 本模块首批接口从API version 7开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。

导入模块

import systemTime from '@ohos.systemTime';

systemTime.setTime

setTime(time : number, callback : AsyncCallback) : void

设置系统时间。

需要权限: ohos.permission.SET_TIME

系统能力: SystemCapability.Miscservices.Time

参数:

参数名类型必填说明
timenumber目标时间戳(ms)。
callbackAsyncCallback回调函数,可以在回调函数中处理接口返回值。

示例:

// time对应的时间为2021-01-20 02:36:25
var time = 1611081385000;
systemTime.setTime(time, (error, data) => {if (error) {console.error(`failed to systemTime.setTime because ` + JSON.stringify(error));return;}console.log(`systemTime.setTime success data : ` + JSON.stringify(data));
});

systemTime.setTime

setTime(time : number) : Promise

设置系统时间。

需要权限: ohos.permission.SET_TIME

系统能力: SystemCapability.MiscServices.Time

参数:

参数名类型必填说明
timenumber目标时间戳(ms)。

返回值:

类型说明
Promise返回的异步回调函数。

示例:

// time对应的时间为2021-01-20 02:36:25
var time = 1611081385000;
systemTime.setTime(time).then((data) => {console.log(`systemTime.setTime success data : ` + JSON.stringify(data));
}).catch((error) => {console.error(`failed to systemTime.setTime because ` + JSON.stringify(error));
});

systemTime.getCurrentTime8+

getCurrentTime(isNano?: boolean, callback: AsyncCallback): void

获取自 Unix 纪元以来经过的时间,使用callback形式返回结果。

系统能力: SystemCapability.MiscServices.Time

参数:

参数名类型必填说明
isNanoboolean如果是true,返回纳秒数;否则返回毫秒数。
callbackAsyncCallback回调函数,返回自 Unix 纪元以来经过的时间。

示例:

systemTime.getCurrentTime(true, (error, data) => {if (error) {console.error(`failed to systemTime.getCurrentTime because ` + JSON.stringify(error));return;}console.log(`systemTime.getCurrentTime success data : ` + JSON.stringify(data));
});

systemTime.getCurrentTime8+

getCurrentTime(isNano?: boolean): Promise

获取自 Unix 纪元以来经过的时间,使用Promise形式返回结果。

系统能力: SystemCapability.MiscServices.Time

参数:

参数名类型必填说明
isNanoboolean如果是true,返回纳秒数;否则返回毫秒数。

返回值:

类型说明
Promise以Promise形式返回结果,返回自 Unix 纪元以来经过的时间。

示例:

systemTime.getCurrentTime().then((data) => {console.log(`systemTime.getCurrentTime success data : ` + JSON.stringify(data));
}).catch((error) => {console.error(`failed to systemTime.getCurrentTime because ` + JSON.stringify(error));
});

systemTime.getRealActiveTime8+

getRealActiveTime(isNano?: boolean, callback: AsyncCallback): void

获取自系统启动以来经过的时间,不包括深度睡眠时间,使用callback形式返回结果。

系统能力: SystemCapability.MiscServices.Time

参数:

参数名类型必填说明
isNanoboolean如果是true,返回纳秒数;否则返回毫秒数。
callbackAsyncCallback回调函数,返回自系统启动以来但不包括度睡眠时间经过的时间。

示例:

systemTime.getRealActiveTime(true, (error, data) => {if (error) {console.error(`failed to systemTime.getRealActiveTimebecause ` + JSON.stringify(error));return;}console.log(`systemTime.getRealActiveTime success data : ` + JSON.stringify(data));
});

systemTime.getRealActiveTime8+

getRealActiveTime(isNano?: boolean): Promise

获取自系统启动以来经过的时间,不包括深度睡眠时间,使用Promise形式返回结果。

系统能力: SystemCapability.MiscServices.Time

参数:

参数名类型必填说明
isNanoboolean如果是true,返回纳秒数;否则返回毫秒数。

返回值:

类型说明
Promise以Promise形式返回结果,返回自系统启动以来经过的时间,但不包括深度睡眠时间。

示例:

systemTime.getRealActiveTime().then((data) => {console.log(`systemTime.getRealActiveTime success data : ` + JSON.stringify(data));
}).catch((error) => {console.error(`failed to systemTime.getRealActiveTime because ` + JSON.stringify(error));
});

systemTime.getRealTime8+

getRealTime(callback: AsyncCallback): void

获取自系统启动以来经过的时间,包括深度睡眠时间,使用callback形式返回结果。

系统能力: SystemCapability.MiscServices.Time

参数:

参数名类型必填说明
isNanoboolean如果是true,返回纳秒数;否则返回毫秒数。
callbackAsyncCallback回调函数,返回自系统启动以来经过的时间,包括深度睡眠时间。

示例:

systemTime.getRealTime(true, (error, data) => {if (error) {console.error(`failed to systemTime.getRealTime because ` + JSON.stringify(error));return;}console.log(`systemTime.getRealTime success data: ` + JSON.stringify(data));
});

systemTime.getRealTime8+

getRealTime(): Promise

获取自系统启动以来经过的时间,包括深度睡眠时间,使用Promise形式返回结果。

系统能力: SystemCapability.MiscServices.Time

参数:

参数名类型必填说明
isNanoboolean如果是true,返回纳秒数;否则返回毫秒数。

返回值:

类型说明
Promise以Promise形式返回结果,返回自系统启动以来经过的时间,包括深度睡眠时间。

示例:

systemTime.getRealTime().then((data) => {console.log(`systemTime.getRealTime success data: ` + JSON.stringify(data));
}).catch((error) => {console.error(`failed to systemTime.getRealTime because ` + JSON.stringify(error));
});

systemTime.setDate

setDate(date: Date, callback: AsyncCallback): void

设置系统日期,使用callback形式返回结果。

需要权限: ohos.permission.SET_TIME

系统能力: SystemCapability.MiscServices.Time

参数:

参数名类型必填说明
dateDate目标日期。
callbackAsyncCallback回调函数,可以在回调函数中处理接口返回值。

示例:

var data = new Date("October 13, 2020 11:13:00");
systemTime.setDate(data,(error, data) => {       if (error) {            console.error('failed to systemTime.setDate because ' + JSON.stringify(error));           return;       
}        console.info('systemTime.setDate success data : ' + JSON.stringify(data));    
});

systemTime.setDate

setDate(date: Date): Promise

设置系统日期,使用Promise形式返回结果。

需要权限: ohos.permission.SET_TIME

系统能力: SystemCapability.MiscServices.Time

参数:

参数名类型必填说明
dateDate目标日期。

返回值:

类型说明
Promise返回的异步回调函数。

示例:

var data = new Date("October 13, 2020 11:13:00"); 
systemTime.setDate(data).then((value) => {        console.log(`systemTime.setDate success data : ` + JSON.stringify(value));    
}).catch((error) => {        console.error(`failed to systemTime.setDate because: ` + JSON.stringify(error));
});

systemTime.getDate8+

getDate(callback: AsyncCallback): void

获取当前系统日期,使用callback形式返回结果。

系统能力: SystemCapability.MiscServices.Time

参数:

参数名类型必填说明
callbackAsyncCallback回调函数,返回当前系统日期。

示例:

systemTime.getDate((error, data) => {if (error) {console.error(`failed to systemTime.getDate because ` + JSON.stringify(error));return;}console.log(`systemTime.getDate success data : ` + JSON.stringify(data));
});

systemTime.getDate8+

getDate(): Promise

获取当前系统日期,使用Promise形式返回结果。

系统能力: SystemCapability.MiscServices.Time

返回值:

类型说明
Promise以Promise形式返回结果,返回当前系统日期。

示例:

systemTime.getDate().then((data) => {console.log(`systemTime.getDate success data : ` + JSON.stringify(data));
}).catch((error) => {console.error(`failed to systemTime.getDate because ` + JSON.stringify(error));
});

systemTime.setTimezone

setTimezone(timezone: string, callback: AsyncCallback): void

设置系统时区。

需要权限: ohos.permission.SET_TIME_ZONE

系统能力: SystemCapability.MiscServices.Time

参数:

参数名类型必填说明
timezonestring系统时区。
callbackAsyncCallback回调函数,可以在回调函数中处理接口返回值。

示例:

systemTime.setTimezone('Asia/Shanghai', (error, data) => {       if (error) {          console.error('failed to systemTime.setTimezone because ' + JSON.stringify(error));         return;       }       console.info('SystemTimePlugin systemTime.setTimezone success data : ' + JSON.stringify(data)); 
});

systemTime.setTimezone

setTimezone(timezone: string): Promise

设置系统时区。

需要权限: ohos.permission.SET_TIME_ZONE

系统能力: SystemCapability.MiscServices.Time

参数:

参数名类型必填说明
timezonestring系统时区。

返回值:

类型说明
Promise返回的异步回调函数。

示例:

systemTime.setTimezone('Asia/Shanghai').then((data) => {        console.log(`systemTime.setTimezone success data : ` + JSON.stringify(data));     
}).catch((error) => {        console.error(`failed to systemTime.setTimezone because: ` + JSON.stringify(error));    
});

systemTime.getTimezone8+

getTimezone(callback: AsyncCallback): void

获取系统时区,使用callback形式返回结果。

系统能力: SystemCapability.MiscServices.Time

参数:

参数名类型必填说明
callbackAsyncCallback回调函数,返回系统时区。

示例:

systemTime.getTimezone((error, data) => {if (error) {console.error(`failed to systemTime.getTimezone because ` + JSON.stringify(error));return;}console.log(`systemTime.getTimezone success data : ` + JSON.stringify(data));
});

systemTime.getTimezone8+

getTimezone(): Promise

获取系统时区,使用Promise形式返回结果。

系统能力: SystemCapability.MiscServices.Time

返回值:

类型说明
Promise以Promise形式返回结果,返回系统时区。

在这里插入图片描述

示例:

systemTime.getTimezone().then((data) => {console.log(`systemTime.getTimezone success data : ` + JSON.stringify(data));
}).catch((error) => {console.error(`failed to systemTime.getTimezone because ` + JSON.stringify(error));
});
http://www.lryc.cn/news/384365.html

相关文章:

  • CVE-2020-26048(文件上传+SQL注入)
  • 【面试题】信息系统安全运维要做什么
  • 引导过程与服务器控制
  • 前置章节-熟悉Python、Numpy、SciPy和matplotlib
  • 在Ubuntu上安装和配置配置服务器防火墙(CSF)的方法
  • Python-井字棋
  • 39.客户端与服务端断开事件handler
  • SSL 之 http只用crt格式证书完成SSL单向认证通信
  • 实训作业-人事资源管理系统
  • Flink 资源静态调度
  • upload-labs第十三关教程
  • 基于springboot实现宠物商城网站管理系统项目【项目源码+论文说明】计算机毕业设计
  • Fragment与ViewModel(MVVM架构)
  • Linux开发讲课16--- 【内存管理】页表映射基础知识2
  • uniapp地图点击获取位置
  • Unity程序开发:1.基本概念及操作
  • 前端新手小白的第一个AI全栈项目---AI聊天室
  • 金升阳电源被制裁,广州顶源电源模块可以完美替换
  • 《数据赋能:一本书讲透数字化营销与运营》—— 从正确的数据观开始
  • JDK 24:Leyden
  • 对于图片转3d人脸方面的研究
  • .NET C# 八股文 代码阅读(一)
  • C++用Crow实现一个简单的Web程序,实现动态页面,向页面中输入数据并展示
  • 南信大尹志聪教授为一作在顶级综合性期刊《Natl. Sci. Rev.》发文:传统梅雨停摆,江南缘何不再多烟雨?
  • 程序员如何用ChatGPT解决常见编程问题:实例解析
  • 初识 SpringMVC,运行配置第一个Spring MVC 程序
  • STM32F1+HAL库+FreeTOTS学习1——FreeRTOS入门
  • 杭州代理记账报税全程托管专业实力全面指南
  • PHP 界的扛把子 Swoole 异步通信利器
  • 40.连接假死-空闲检测-发送心跳