HarmonyOS:获取系统时间及系统时区

本模块主要由系统时间和系统时区功能组成。开发者可以设置、获取系统时间及系统时区。
说明 本模块首批接口从API version 9开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。

一、导入模块

javascript 复制代码
import { systemDateTime } from '@kit.BasicServicesKit';

二、TimeType10+

定义获取时间的枚举类型。
系统能力: SystemCapability.MiscServices.Time

名称 值 说明
STARTUP 0 自系统启动以来经过的毫秒数,包括深度睡眠时间。
ACTIVE 1 自系统启动以来经过的毫秒数,不包括深度睡眠时间。

三、systemDateTime.getCurrentTime(deprecated)

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

获取自Unix纪元以来经过的时间,使用callback异步回调。
说明 从API Version 12开始不再维护,建议使用systemDateTime.getTime10+替代。

系统能力: SystemCapability.MiscServices.Time

参数:

参数名 类型 必填 说明
isNano boolean 是 返回结果是否为纳秒数。 - true:表示返回结果为纳秒数(ns)。 - false:表示返回结果为毫秒数(ms)。
callback AsyncCallback<number> 是 回调函数,返回自Unix纪元以来经过的时间戳。

错误码:

以下错误码的详细介绍请参见时间时区错误码。

错误码ID 错误信息
401 Parameter error. Possible causes: 1.Incorrect parameter types.

效果图

javascript 复制代码
function testGetCurrentTime() {
  try {
    systemDateTime.getCurrentTime(false, (error: BusinessError, time: number) => {
      if (error) {
        console.info(`testGetCurrentTime 获取当前时间出错了. message: ${error.message}, code: ${error.code}`);
        return;
      }
      console.info(`testGetCurrentTime 获取当前时间成功 currentTime : ${time}`);
    })

  } catch (e) {
    let error = e as BusinessError;
    console.info(`testGetCurrentTime 获取当前时间发生异常 message: ${error.message}, code: ${error.code}`);
  }
}

function testGetTime() {
  try {
    let time = systemDateTime.getTime(false)
    console.info(`testGetTime 获取当前时间成功 currentTime : ${time}`);
  } catch (e) {
    let error = e as BusinessError;
    console.info(`testGetTime 获取当前时间发生异常 message: ${error.message}, code: ${error.code}`);
  }
}

四、systemDateTime.getCurrentTime(deprecated)

getCurrentTime(callback: AsyncCallback): void

获取自Unix纪元以来经过的时间,使用callback异步回调。
说明 从API Version 12开始不再维护,建议使用systemDateTime.getTime10+替代。
系统能力: SystemCapability.MiscServices.Time

参数:

参数名 类型 必填 说明
callback AsyncCallback 是 回调函数,返回自Unix纪元以来经过的时间戳为毫秒数(ms)。

错误码:

以下错误码的详细介绍请参见时间时区错误码。

错误码ID 错误信息
401 Parameter error. Possible causes: 1.Incorrect parameter types.

效果图

javascript 复制代码
function testGetCurrentTime2() {
  try {
    //回调函数,返回自Unix纪元以来经过的时间戳为毫秒数(ms)。
    systemDateTime.getCurrentTime((error: BusinessError, time: number) => {
      if (error) {
        console.info(`testGetCurrentTime2 获取当前时间出错了. message: ${error.message}, code: ${error.code}`);
        return;
      }
      console.info(`testGetCurrentTime2 获取当前时间成功 currentTime : ${time}`);
    });
  } catch(e) {
    let error = e as BusinessError;
    console.info(`testGetCurrentTime2 获取当前时间发生异常 message: ${error.message}, code: ${error.code}`);
  }
}

五、systemDateTime.getCurrentTime(deprecated)

getCurrentTime(isNano?: boolean): Promise

获取自Unix纪元以来经过的时间,使用Promise异步回调。
说明 从API Version 12开始不再维护,建议使用systemDateTime.getTime10+替代。
系统能力: SystemCapability.MiscServices.Time

参数:

参数名 类型 必填 说明
isNano boolean 否 返回结果是否为纳秒数,默认值为false。 - true:表示返回结果为纳秒数(ns)。 - false:表示返回结果为毫秒数(ms)。

返回值:

类型 说明
Promise<number> Promise对象,返回自Unix纪元以来经过的时间戳。

错误码:

以下错误码的详细介绍请参见时间时区错误码。

错误码ID 错误信息
401 Parameter error. Possible causes: 1.Incorrect parameter types.

效果图

javascript 复制代码
function testGetCurrentTime3() {
  try {
    systemDateTime.getCurrentTime().then((time: number) => {
      console.info(`testGetCurrentTime3 获取当前时间成功 currentTime : ${time}`);
    }).catch((error: BusinessError) => {
      console.info(`testGetCurrentTime3  获取当前时间发生错误 message: ${error.message}, code: ${error.code}`);
    });
  } catch (e) {
    let error = e as BusinessError
    console.info(`testGetCurrentTime3 获取当前时间发生异常 message: ${error.message}, code: ${error.code}`);
  }
}

六、systemDateTime.getDate(deprecated)

getDate(callback: AsyncCallback): void

获取当前系统日期,使用callback异步回调。
说明 从API version 9开始支持,从API 10开始废弃。建议使用new Date()替代,new Date()返回Date实例对象。
系统能力: SystemCapability.MiscServices.Time

参数:

参数名 类型 必填 说明
callback AsyncCallback<Date> 是 回调函数,返回当前系统日期。

错误码:

以下错误码的详细介绍请参见时间时区错误码。

错误码ID 错误信息
401 Parameter error. Possible causes: 1.System error.

效果图

javascript 复制代码
function testDate() {
  let date = new Date();
  console.info(`testDate 获取当前系统日期成功 date : ${date}`);
}

function testGetDate1() {
  try {
    systemDateTime.getDate((error: BusinessError, date: Date) => {
      if (error) {
        console.info(`testGetDate1 获取当前系统日期 发生错误 message: ${error.message}, code: ${error.code}`);
        return;
      }
      console.info(`testGetDate1 获取当前系统日期成功 date : ${date}`);
    });
  } catch (e) {
    let error = e as BusinessError;
    console.info(`testGetDate1 获取当前系统日期 发生异常 message: ${error.message}, code: ${error.code}`);
  }
}

七、systemDateTime.getDate(deprecated)

getDate(): Promise

获取当前系统日期,使用Promise异步回调。
说明 从API version 9开始支持,从API 10开始废弃。建议使用new Date()替代,new Date()返回Date实例对象。
系统能力: SystemCapability.MiscServices.Time

返回值:

类型 说明
Promise<Date> Promise对象,返回当前系统日期。

错误码:

以下错误码的详细介绍请参见时间时区错误码。

错误码ID 错误信息
401 Parameter error. Possible causes: 1.System error.
javascript 复制代码
function testGetDate2() {
  try {
    systemDateTime.getDate().then((date: Date) => {
      console.info(`testGetDate2 获取当前系统日期成功 date : ${date}`);
    }).catch((error: BusinessError) => {
      console.info(`testGetDate2 获取当前系统日期 发生错误  message: ${error.message}, code: ${error.code}`);
    });
  } catch (e) {
    let error = e as BusinessError;
    console.info(`testGetDate2 获取当前系统日期 发生异常 message: ${error.message}, code: ${error.code}`);
  }
}

八、systemDateTime.getTimezone

getTimezone(callback: AsyncCallback): void

获取系统时区,使用callback异步回调。
系统能力: SystemCapability.MiscServices.Time

参数:

参数名 类型 必填 说明
allback AsyncCallback<string> 是 回调函数,返回系统时区。具体可见支持的系统时区 。

效果图

javascript 复制代码
function testGetTimezone1() {
  try {
    systemDateTime.getTimezone((error: BusinessError, data: string) => {
      if (error) {
        console.info(`testGetTimezone1 获取系统时区 发生错误 message: ${error.message}, code: ${error.code}`);
        return;
      }
      console.info(`testGetTimezone1 获取系统时区 成功 timezone : ${data}`);
    });
  } catch (e) {
    let error = e as BusinessError;
    console.info(`testGetTimezone1 获取系统时区 发生异常 message: ${error.message}, code: ${error.code}`);
  }
}

九、systemDateTime.getTimezone

getTimezone(): Promise

获取系统时区,使用Promise异步回调。
系统能力: SystemCapability.MiscServices.Time

返回值:

类型 说明
Promise<string> Promise对象,返回系统时区。具体可见支持的系统时区 。
javascript 复制代码
function testGetTimezone2() {
  try {
    systemDateTime.getTimezone().then((data: string) => {
      console.info(`testGetTimezone2 获取系统时区 成功 timezone : ${data}`);
    }).catch((error: BusinessError) => {
      console.info(`testGetTimezone2 获取系统时区 发生错误 message: ${error.message}, code: ${error.code}`);
    });
  } catch (e) {
    let error = e as BusinessError;
    console.info(`testGetTimezone2 获取系统时区 发生异常 message: ${error.message}, code: ${error.code}`);
  }
}

十、systemDateTime.getTimezoneSync10+

getTimezoneSync(): string

获取系统时区,使用同步方式。
系统能力: SystemCapability.MiscServices.Time

返回值:

类型 说明
string 返回系统时区。具体可见支持的系统时区 。

效果图

javascript 复制代码
function testGetTimezoneSync() {
  try {
    let timezone = systemDateTime.getTimezoneSync();
    console.info(`testGetTimezoneSync  获取系统时区 成功 timezone: ${timezone}`);
  } catch (e) {
    let error = e as BusinessError;
    console.info(`testGetTimezoneSync 获取系统时区 发生异常 message: ${error.message}, code: ${error.code}`);
  }
}

十一、支持的系统时区

支持的系统时区及各时区与0时区相比的偏移量(单位:h)可见下表。

时区 偏移量
Antarctica/McMurdo 12
America/Argentina/Buenos_Aires -3
Australia/Sydney 10
America/Noronha -2
America/St_Johns -3
Africa/Kinshasa 1
America/Santiago -3
Asia/Shanghai 8
Asia/Nicosia 3
Europe/Berlin 2
America/Guayaquil -5
Europe/Madrid 2
Pacific/Pohnpei 11
America/Godthab -2
Asia/Jakarta 7
Pacific/Tarawa 12
Asia/Almaty 6
Pacific/Majuro 12
Asia/Ulaanbaatar 8
America/Mexico_City -5
Asia/Kuala_Lumpur 8
Pacific/Auckland 12
Pacific/Tahiti -10
Pacific/Port_Moresby 10
Asia/Gaza 3
Europe/Lisbon 1
Europe/Moscow 3
Europe/Kiev 3
Pacific/Wake 12
America/New_York -4
Asia/Tashkent 5
相关推荐
c2892480843s6 小时前
【鸿蒙心迹】网络请求封装实战:RCP 选型、拦截器与弱网预算
移动开发·网络请求·harmonyos·鸿蒙·rcp
HarmonyOS_SDK11 小时前
【剪贴板体验优化】前置预校验,航旅纵横权限弹窗体验更智能
harmonyos
贾伟康12 小时前
【共创稿事节】HarmonyOS 7.0 Progress进度体系:从接入到生产验证
性能优化·实战·harmonyos·arkts·arkui
贾伟康19 小时前
HarmonyOS API 26 HMAF Skill 端侧 A2A 消息去重与上下文最小化
harmonyos·arkts·skill·a2a·hmaf
贾伟康19 小时前
HarmonyOS API 26 HMAF Skill 多 Skill 冲突仲裁与可恢复执行
harmonyos·arkts·智能体·skill·hmaf
钉先生贾20 小时前
毕业项目:从上游更新到回归发布的完整维护闭环
华为·harmonyos
行者-全栈开发20 小时前
【鸿蒙心迹】权限申请全流程——从声明到用户授权的完整链路(HarmonyOS 7.x)
华为·harmonyos
KevinChaos2 天前
阳台小花园开发手记 01|HarmonyOS 7.0 互动卡片:小花园为何要两套界面
华为·harmonyos
Ranger09292 天前
下一代鸿蒙打包工具链:arkdown
harmonyos
程序猿追2 天前
一个工程面向手机与二合一:HarmonyOS 6 部署配置和本地手记
华为·智能手机·harmonyos