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
相关推荐
Hi-Dison15 分钟前
Open HarmonyOS 5.0 分布式软总线子系统 (DSoftBus) 详细设计与运行分析报告
分布式·华为·harmonyos
李游Leo3 小时前
HarmonyOS:ArkTS Path 组件自学指南
harmonyos
zhousg3 小时前
HarmonyOS NEXT AI基础语音服务-文章播报
harmonyos
zhousg3 小时前
HarmonyOS NEXT AI基础视觉服务-文字识别
harmonyos
小藤神3 小时前
鸿蒙ArkTS 视频相关 视频播放、直播视频、XComponent和typeNode多方案实现画中画功能开发
华为·harmonyos·arkts
中雨20255 小时前
HarmonyOS NEXT函数和自定义构建函数
harmonyos
博睿谷IT99_5 小时前
华为HCIE鸿蒙应用开发认证靠谱吗?
华为认证·hcie·harmonyos·鸿蒙·鸿蒙系统
二流小码农6 小时前
鸿蒙开发:Canvas绘制之画笔对象Brush
android·ios·harmonyos
二流小码农7 小时前
鸿蒙开发:事件订阅EventHub
android·ios·harmonyos
失落的多巴胺7 小时前
嵌入式项目:基于QT与海思HI3861开发板设计的鸿蒙智能车
开发语言·嵌入式硬件·qt·学习·harmonyos