1 Star 0 Fork 5.1K

youguilin / docs

forked from OpenHarmony / docs 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
时间设置.md 9.30 KB
一键复制 编辑 原始数据 按行查看 历史
mamingshuai 提交于 2021-06-02 01:00 . update OpenHarmony 2.0 Canary

时间设置

导入模块

import systemTime from '@ohos.systemTime';

权限列表

无。

setTime(number, AsyncCallback<boolean>)

设置系统时间

参数:

参数名

类型

必填

说明

time

number

目标时间戳(毫秒)。

callback

AsyncCallback<boolean>

回调函数,可以在回调函数中处理接口返回值。

  • 返回值为true,表示设置系统时间成功;
  • 返回值为false,表示设置系统时间失败。

示例:

// time对应的时间为2021-01-20 02:36:25
var time = 1611081385000;
systemTime.setTime(time, (err, value) => {
    if (err) {
        // 设置时间失败,err非空,value为空。
        console.error(`failed to systemTime.setTime because ${err.message}`);
        return;
    }

    // 设置时间成功,err为空,value非空。
    console.log(`success to systemTime.setTime: ${value}`);
});

setTime(number) : Promise<boolean>

设置系统时间

参数:

参数名

类型

必填

说明

time

number

目标时间戳(毫秒)。

返回值:

类型

说明

Promise<boolean>

  • 返回值为true,表示设置系统时间成功;
  • 返回值为false,表示设置系统时间失败。

示例:

// time对应的时间为2021-01-20 02:36:25
var time = 1611081385000;
systemTime.setTime(time)
    .then((value) => {
        // 设置时间成功
        console.log(`success to systemTime.setTime: ${value}`);
    }).catch((err) => {
        // 设置时间失败
        console.error(`failed to systemTime.setTime because ${err.message}`);
    });
1
https://gitee.com/yougl/docs.git
git@gitee.com:yougl/docs.git
yougl
docs
docs
master

搜索帮助

53164aa7 5694891 3bd8fe86 5694891