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

本模块主要由系统时间和系统时区功能组成。开发者可以设置、获取系统时间及系统时区。

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

一、导入模块

import { systemDateTime } from '@kit.BasicServicesKit';

二、TimeType10+

定义获取时间的枚举类型。

系统能力: SystemCapability.MiscServices.Time

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

三、systemDateTime.getCurrentTime(deprecated)

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

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

说明
从API Version 12开始不再维护,建议使用systemDateTime.getTime10+替代。

系统能力: SystemCapability.MiscServices.Time

参数:

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

错误码:

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

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

效果图

在这里插入图片描述

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

参数:

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

错误码:

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

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

效果图

在这里插入图片描述

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

参数:

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

返回值:

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

错误码:

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

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

效果图

在这里插入图片描述

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

参数:

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

错误码:

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

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

效果图

在这里插入图片描述

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错误信息
401Parameter error. Possible causes: 1.System error.
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

参数:

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

效果图

在这里插入图片描述

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对象,返回系统时区。具体可见支持的系统时区
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返回系统时区。具体可见支持的系统时区

效果图

在这里插入图片描述

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/Sydney10
America/Noronha-2
America/St_Johns-3
Africa/Kinshasa1
America/Santiago-3
Asia/Shanghai8
Asia/Nicosia3
Europe/Berlin2
America/Guayaquil-5
Europe/Madrid2
Pacific/Pohnpei11
America/Godthab-2
Asia/Jakarta7
Pacific/Tarawa12
Asia/Almaty6
Pacific/Majuro12
Asia/Ulaanbaatar8
America/Mexico_City-5
Asia/Kuala_Lumpur8
Pacific/Auckland12
Pacific/Tahiti-10
Pacific/Port_Moresby10
Asia/Gaza3
Europe/Lisbon1
Europe/Moscow3
Europe/Kiev3
Pacific/Wake12
America/New_York-4
Asia/Tashkent5
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值