harmony 鸿蒙@ohos.systemDateTime (System Time and Time Zone)

2023-02-03 浏览 (869)

@ohos.systemDateTime (System Time and Time Zone)

The systemDateTime module provides system time and time zone features. You can use the APIs of this module to set and obtain the system time and time zone.

NOTE

The initial APIs of this module are supported since API version 9. Newly added APIs will be marked with a superscript to indicate their earliest API version.

Modules to Import

import systemDateTime from '@ohos.systemDateTime';

TimeType10+

Enumerates the types of time to obtain.

System capability: SystemCapability.MiscServices.Time

NameValueDescription
STARTUP0Number of milliseconds elapsed since system startup, including the deep sleep time.
ACTIVE1Number of milliseconds elapsed since system startup, excluding the deep sleep time.

systemDateTime.setTime

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

Sets the system time. This API uses an asynchronous callback to return the result.

System API: This is a system API.

System capability: SystemCapability.MiscServices.Time

Required permissions: ohos.permission.SET_TIME

Parameters

NameTypeMandatoryDescription
timenumberYesTimestamp to set, in milliseconds.
callbackAsyncCallback<void>YesCallback used to return the result.

Example

import { BusinessError } from '@ohos.base';

// Set the system time to 2021-01-20 02:36:25.
let time = 1611081385000;
try {
  systemDateTime.setTime(time, (error: BusinessError) => {
    if (error) {
      console.info(`Failed to set time. message: ${error.message}, code: ${error.code}`);
      return;
    }
    console.info(`Succeeded in setting time`);
  });
} catch(e) {
  let error = e as BusinessError;
  console.info(`Failed to set time. message: ${error.message}, code: ${error.code}`);
}

systemDateTime.setTime

setTime(time : number) : Promise<void>

Sets the system time. This API uses a promise to return the result.

System API: This is a system API.

System capability: SystemCapability.MiscServices.Time

Required permissions: ohos.permission.SET_TIME

Parameters

NameTypeMandatoryDescription
timenumberYesTimestamp to set, in milliseconds.

Return value

TypeDescription
Promise<void>Promise that returns no value.

Example

import { BusinessError } from '@ohos.base';

// Set the system time to 2021-01-20 02:36:25.
let time = 1611081385000;
try {
  systemDateTime.setTime(time).then(() => {
    console.info(`Succeeded in setting time.`);
  }).catch((error: BusinessError) => {
    console.info(`Failed to set time. message: ${error.message}, code: ${error.code}`);
  });
} catch(e) {
  let error = e as BusinessError;
  console.info(`Failed to set time. message: ${error.message}, code: ${error.code}`);
}

systemDateTime.getCurrentTime

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

Obtains the time elapsed since the Unix epoch. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.MiscServices.Time

Parameters

NameTypeMandatoryDescription
isNanobooleanYesWhether the time to return is in nanoseconds.
- true: The time to return is in nanoseconds.
- false: The time to return is in milliseconds.
callbackAsyncCallback<number>YesCallback used to return the time elapsed since the Unix epoch.

Example

import { BusinessError } from '@ohos.base';

try {
  systemDateTime.getCurrentTime(true, (error: BusinessError, time: number) => {
    if (error) {
      console.info(`Failed to get currentTime. message: ${error.message}, code: ${error.code}`);
      return;
    }
    console.info(`Succeeded in getting currentTime : ${time}`);
  });
} catch(e) {
  let error = e as BusinessError;
  console.info(`Failed to get currentTime. message: ${error.message}, code: ${error.code}`);
}

systemDateTime.getCurrentTime

getCurrentTime(callback: AsyncCallback<number>): void

Obtains the time elapsed since the Unix epoch. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.MiscServices.Time

Parameters

NameTypeMandatoryDescription
callbackAsyncCallback<number>YesCallback used to return the time elapsed since the Unix epoch, in milliseconds.

Example

import { BusinessError } from '@ohos.base';

try {
  systemDateTime.getCurrentTime((error: BusinessError, time: number) => {
    if (error) {
      console.info(`Failed to get currentTime. message: ${error.message}, code: ${error.code}`);
      return;
    }
    console.info(`Succeeded in getting currentTime : ${time}`);
  });
} catch(e) {
  let error = e as BusinessError;
  console.info(`Failed to get currentTime. message: ${error.message}, code: ${error.code}`);
}

systemDateTime.getCurrentTime

getCurrentTime(isNano?: boolean): Promise<number>

Obtains the time elapsed since the Unix epoch. This API uses a promise to return the result.

System capability: SystemCapability.MiscServices.Time

Parameters

NameTypeMandatoryDescription
isNanobooleanNoWhether the time to return is in nanoseconds. The default value is false.
- true: The time to return is in nanoseconds.
- false: The time to return is in milliseconds.

Return value

TypeDescription
Promise<number>Promise used to return the time elapsed since the Unix epoch.

Example

import { BusinessError } from '@ohos.base';

try {
  systemDateTime.getCurrentTime().then((time: number) => {
    console.info(`Succeeded in getting currentTime : ${time}`);
  }).catch((error: BusinessError) => {
    console.info(`Failed to get currentTime. message: ${error.message}, code: ${error.code}`);
  });
} catch(e) {
  let error = e as BusinessError;
  console.info(`Failed to get currentTime. message: ${error.message}, code: ${error.code}`);
}

systemDateTime.getRealActiveTime

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

Obtains the time elapsed since system startup, excluding the deep sleep time. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.MiscServices.Time

Parameters

NameTypeMandatoryDescription
isNanobooleanYesWhether the time to return is in nanoseconds.
- true: The time to return is in nanoseconds.
- false: The time to return is in milliseconds.
callbackAsyncCallback<number>YesCallback used to return the time.

Example

import { BusinessError } from '@ohos.base';

try {
  systemDateTime.getRealActiveTime(true, (error: BusinessError, time: number) => {
    if (error) {
      console.info(`Failed to get real active time. message: ${error.message}, code: ${error.code}`);
      return;
    }
    console.info(`Succeeded in getting real active time : ${time}`);
  });
} catch(e) {
  let error = e as BusinessError;
  console.info(`Failed to get real active time. message: ${error.message}, code: ${error.code}`);
}

systemDateTime.getRealActiveTime

getRealActiveTime(callback: AsyncCallback<number>): void

Obtains the time elapsed since system startup, excluding the deep sleep time. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.MiscServices.Time

Parameters

NameTypeMandatoryDescription
callbackAsyncCallback<number>YesCallback used to return the time.

Example

import { BusinessError } from '@ohos.base';

try {
  systemDateTime.getRealActiveTime((error: BusinessError, time: number) => {
    if (error) {
      console.info(`Failed to get real active time. message: ${error.message}, code: ${error.code}`);
      return;
    }
    console.info(`Succeeded in getting real active time : ${time}`);
  });
} catch(e) {
  let error = e as BusinessError;
  console.info(`Failed to get real active time. message: ${error.message}, code: ${error.code}`);
}

systemDateTime.getRealActiveTime

getRealActiveTime(isNano?: boolean): Promise<number>

Obtains the time elapsed since system startup, excluding the deep sleep time. This API uses a promise to return the result.

System capability: SystemCapability.MiscServices.Time

Parameters

NameTypeMandatoryDescription
isNanobooleanNoWhether the time to return is in nanoseconds. The default value is false.
- true: The time to return is in nanoseconds.
- false: The time to return is in milliseconds.

Return value

TypeDescription
Promise<number>Promise used to return the time elapsed since system startup, excluding the deep sleep time.

Example

import { BusinessError } from '@ohos.base';

try {
  systemDateTime.getRealActiveTime().then((time: number) => {
    console.info(`Succeeded in getting real active time : ${time}`);
  }).catch((error: BusinessError) => {
    console.info(`Failed to get real active time. message: ${error.message}, code: ${error.code}`);
  });
} catch(e) {
  let error = e as BusinessError;
  console.info(`Failed to get real active time. message: ${error.message}, code: ${error.code}`);
}

systemDateTime.getRealTime

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

Obtains the time elapsed since system startup, including the deep sleep time. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.MiscServices.Time

Parameters

NameTypeMandatoryDescription
isNanobooleanYesWhether the time to return is in nanoseconds.
- true: The time to return is in nanoseconds.
- false: The time to return is in milliseconds.
callbackAsyncCallback<number>YesCallback used to return the time.

Example

import { BusinessError } from '@ohos.base';

try {
  systemDateTime.getRealTime(true, (error: BusinessError, time: number) => {
    if (error) {
      console.info(`Failed to get real time. message: ${error.message}, code: ${error.code}`);
      return;
    }
    console.info(`Succeeded in getting real time : ${time}`);
  });
} catch(e) {
  let error = e as BusinessError;
  console.info(`Failed to get real time. message: ${error.message}, code: ${error.code}`);
}

systemDateTime.getRealTime

getRealTime(callback: AsyncCallback<number>): void

Obtains the time elapsed since system startup, including the deep sleep time. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.MiscServices.Time

Parameters

NameTypeMandatoryDescription
callbackAsyncCallback<number>YesCallback used to return the time.

Example

import { BusinessError } from '@ohos.base';

try {
  systemDateTime.getRealTime((error: BusinessError, time: number) => {
    if (error) {
      console.info(`Failed to get real time. message: ${error.message}, code: ${error.code}`);
      return;
    }
    console.info(`Succeeded in getting real time : ${time}`);
  });
} catch(e) {
  let error = e as BusinessError;
  console.info(`Failed to get real time. message: ${error.message}, code: ${error.code}`);
}

systemDateTime.getRealTime

getRealTime(isNano?: boolean): Promise<number>

Obtains the time elapsed since system startup, including the deep sleep time. This API uses a promise to return the result.

System capability: SystemCapability.MiscServices.Time

Parameters

NameTypeMandatoryDescription
isNanobooleanNoWhether the time to return is in nanoseconds. The default value is false.
- true: The time to return is in nanoseconds.
- false: The time to return is in milliseconds.

Return value

TypeDescription
Promise<number>Promise used to return the time elapsed since system startup, including the deep sleep time.

Example

import { BusinessError } from '@ohos.base';

try {
  systemDateTime.getRealTime().then((time: number) => {
    console.info(`Succeeded in getting real time : ${time}`);
  }).catch((error: BusinessError) => {
    console.info(`Failed to get real time. message: ${error.message}, code: ${error.code}`);
  });
} catch(e) {
  let error = e as BusinessError;
  console.info(`Failed to get real time. message: ${error.message}, code: ${error.code}`);
}

systemDateTime.getTime10+

getTime(isNanoseconds?: boolean): number

Obtains the time elapsed since the Unix epoch. This API returns the result synchronously.

System capability: SystemCapability.MiscServices.Time

Parameters

NameTypeMandatoryDescription
isNanosecondsbooleanNoWhether the time to return is in nanoseconds.
- true: The time to return is in nanoseconds.
- false: The time to return is in milliseconds.
Default value: false

Return value

TypeDescription
numberTime elapsed since the Unix epoch.

Example

try {
  let time = systemDateTime.getTime(true)
} catch(e) {
  let error = e as BusinessError;
  console.info(`Failed to get time. message: ${error.message}, code: ${error.code}`);
}

systemDateTime.getUptime10+

getUptime(timeType: TimeType, isNanoseconds?: boolean): number

Obtains the time elapsed since system startup. This API returns the result synchronously.

System capability: SystemCapability.MiscServices.Time

Parameters

NameTypeMandatoryDescription
timeTypeTimeTypeYesType of the time to obtain.
isNanosecondsbooleanNoWhether the time to return is in nanoseconds.
- true: The time to return is in nanoseconds.
- false: The time to return is in milliseconds.
Default value: false

Return value

TypeDescription
numberTime elapsed since system startup.

Example

try {
  let time = systemDateTime.getUptime(systemDateTime.TimeType.ACTIVE, false);
} catch(e) {
  let error = e as BusinessError;
  console.info(`Failed to get uptime. message: ${error.message}, code: ${error.code}`);
}

systemDateTime.setDate(deprecated)

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

Sets the system date. This API uses an asynchronous callback to return the result.

NOTE

This API is supported since API version 9 and deprecated since API version 10. You are advised to use systemDateTime.setTime instead.

System API: This is a system API.

System capability: SystemCapability.MiscServices.Time

Required permissions: ohos.permission.SET_TIME

Parameters

NameTypeMandatoryDescription
dateDateYesTarget date to set.
callbackAsyncCallback<void>YesCallback used to return the result.

Example

import { BusinessError } from '@ohos.base';

let date = new Date();
try {
  systemDateTime.setDate(date, (error: BusinessError) => {
    if (error) {
      console.info(`Failed to set date. message: ${error.message}, code: ${error.code}`);
      return;
    }
    console.info(`Succeeded in setting date.`);
  });
} catch(e) {
  let error = e as BusinessError;
  console.info(`Failed to set date. message: ${error.message}, code: ${error.code}`);
}

systemDateTime.setDate(deprecated)

setDate(date: Date): Promise<void>

Sets the system date. This API uses a promise to return the result.

NOTE

This API is supported since API version 9 and deprecated since API version 10. You are advised to use systemDateTime.setTime instead.

System API: This is a system API.

System capability: SystemCapability.MiscServices.Time

Required permissions: ohos.permission.SET_TIME

Parameters

NameTypeMandatoryDescription
dateDateYesTarget date to set.

Return value

TypeDescription
Promise<void>Promise that returns no value.

Example

import { BusinessError } from '@ohos.base';

let date = new Date(); 
try {
  systemDateTime.setDate(date).then(() => {
    console.info(`Succeeded in setting date.`);
  }).catch((error: BusinessError) => {
    console.info(`Failed to set date. message: ${error.message}, code: ${error.code}`);
  });
} catch(e) {
  let error = e as BusinessError;
  console.info(`Failed to set date. message: ${error.message}, code: ${error.code}`);
}

systemDateTime.getDate(deprecated)

getDate(callback: AsyncCallback<Date>): void

Obtains the current system date. This API uses an asynchronous callback to return the result.

NOTE

This API is supported since API version 9 and deprecated since API version 10. You are advised to use new Date() instead, which returns a Date object.

System capability: SystemCapability.MiscServices.Time

Parameters

NameTypeMandatoryDescription
callbackAsyncCallback<Date>YesCallback used to return the current system date.

Example

import { BusinessError } from '@ohos.base';

try {
  systemDateTime.getDate((error: BusinessError, date: Date) => {
    if (error) {
      console.info(`Failed to get date. message: ${error.message}, code: ${error.code}`);
      return;
    }
    console.info(`Succeeded in getting date : ${date}`);;
  });
} catch(e) {
  let error = e as BusinessError;
  console.info(`Failed to get date. message: ${error.message}, code: ${error.code}`);
}

systemDateTime.getDate(deprecated)

getDate(): Promise<Date>

Obtains the current system date. This API uses a promise to return the result.

NOTE

This API is supported since API version 9 and deprecated since API version 10. You are advised to use new Date() instead, which returns a Date object.

System capability: SystemCapability.MiscServices.Time

Return value

TypeDescription
Promise<Date>Promise used to return the current system date.

Example

import { BusinessError } from '@ohos.base';

try {
  systemDateTime.getDate().then((date: Date) => {
    console.info(`Succeeded in getting date : ${date}`);
  }).catch((error: BusinessError) => {
    console.info(`Failed to get date. message: ${error.message}, code: ${error.code}`);
  });
} catch(e) {
  let error = e as BusinessError;
  console.info(`Failed to get date. message: ${error.message}, code: ${error.code}`);
}

systemDateTime.setTimezone

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

Sets the system time zone. This API uses an asynchronous callback to return the result.

System API: This is a system API.

System capability: SystemCapability.MiscServices.Time

Required permissions: ohos.permission.SET_TIME_ZONE

Parameters

NameTypeMandatoryDescription
timezonestringYesSystem time zone to set. For details, see Supported System Time Zones.
callbackAsyncCallback<void>YesCallback used to return the result.

Example

import { BusinessError } from '@ohos.base';

try {
  systemDateTime.setTimezone('Asia/Shanghai', (error: BusinessError) => {
    if (error) {
      console.info(`Failed to set timezone. message: ${error.message}, code: ${error.code}`);
      return;
    }
    console.info(`Succeeded in setting timezone.`);
  });
} catch(e) {
  let error = e as BusinessError;
  console.info(`Failed to set timezone. message: ${error.message}, code: ${error.code}`);
}

systemDateTime.setTimezone

setTimezone(timezone: string): Promise<void>

Sets the system time zone. This API uses a promise to return the result.

System API: This is a system API.

System capability: SystemCapability.MiscServices.Time

Required permissions: ohos.permission.SET_TIME_ZONE

Parameters

NameTypeMandatoryDescription
timezonestringYesSystem time zone to set. For details, see Supported System Time Zones.

Return value

TypeDescription
Promise<void>Promise that returns no value.

Example

import { BusinessError } from '@ohos.base';

try {
  systemDateTime.setTimezone('Asia/Shanghai').then(() => {
    console.info(`Succeeded in setting timezone.`);
  }).catch((error: BusinessError) => {
    console.info(`Failed to set timezone. message: ${error.message}, code: ${error.code}`);
  });
} catch(e) {
  let error = e as BusinessError;
  console.info(`Failed to set timezone. message: ${error.message}, code: ${error.code}`);
}

systemDateTime.getTimezone

getTimezone(callback: AsyncCallback<string>): void

Obtains the system time zone. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.MiscServices.Time

Parameters

NameTypeMandatoryDescription
callbackAsyncCallback<string>YesCallback used to return the system time zone. For details, see Supported System Time Zones.

Example

import { BusinessError } from '@ohos.base';

try {
  systemDateTime.getTimezone((error: BusinessError, data: string) => {
    if (error) {
      console.info(`Failed to get timezone. message: ${error.message}, code: ${error.code}`);
      return;
    }
    console.info(`Succeeded in get timezone : ${data}`);;
  });
} catch(e) {
  let error = e as BusinessError;
  console.info(`Failed to get timezone. message: ${error.message}, code: ${error.code}`);
}

systemDateTime.getTimezone

getTimezone(): Promise<string>

Obtains the system time zone. This API uses a promise to return the result.

System capability: SystemCapability.MiscServices.Time

Return value

TypeDescription
Promise<string>Promise used to return the system time zone. For details, see Supported System Time Zones.

Example

import { BusinessError } from '@ohos.base';

try {
  systemDateTime.getTimezone().then((data: string) => {
    console.info(`Succeeded in getting timezone: ${data}`);
  }).catch((error: BusinessError) => {
    console.info(`Failed to get timezone. message: ${error.message}, code: ${error.code}`);
  });
} catch(e) {
  let error = e as BusinessError;
  console.info(`Failed to get timezone. message: ${error.message}, code: ${error.code}`);
}

systemDateTime.getTimezoneSync10+

getTimezoneSync(): string

Obtain the system time zone. This API returns the result synchronously.

System capability: SystemCapability.MiscServices.Time

Return value

TypeDescription
stringSystem time zone. For details, see Supported System Time Zones.

Example

try {
  let timezone = systemDateTime.getTimezoneSync();
} catch(e) {
  let error = e as BusinessError;
  console.info(`Failed to get timezone. message: ${error.message}, code: ${error.code}`);
}

Supported System Time Zones

The following table lists the supported system time zones and the respective offset (unit: h) between each time zone and time zone 0.

Time ZoneOffset
Antarctica/McMurdo12
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

你可能感兴趣的鸿蒙文章

harmony 鸿蒙APIs

harmony 鸿蒙System Common Events (To Be Deprecated Soon)

harmony 鸿蒙System Common Events

harmony 鸿蒙API Reference Document Description

harmony 鸿蒙Enterprise Device Management Overview (for System Applications Only)

harmony 鸿蒙BundleStatusCallback

harmony 鸿蒙@ohos.bundle.innerBundleManager (innerBundleManager)

harmony 鸿蒙@ohos.distributedBundle (Distributed Bundle Management)

harmony 鸿蒙@ohos.bundle (Bundle)

harmony 鸿蒙@ohos.enterprise.EnterpriseAdminExtensionAbility (EnterpriseAdminExtensionAbility)

  • 所属分类: 后端技术
  • 本文标签: 软件 鸿蒙
  • 版权声明: 本文链接 https://seaxiang.com/blog/1f50de32be0648a18188bf29d965c12a