openharmony 鸿蒙 js-apis-system-time

2025-06-12 浏览 (1)

@ohos.systemTime (System Time and Time Zone)

The systemTime 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 APIs of this module are no longer maintained since API version 9. You are advised to use @ohos.systemDateTime (system time and time zone).
  • The initial APIs of this module are supported since API version 7. Newly added APIs will be marked with a superscript to indicate their earliest API version.

Modules to Import

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

systemTime.getCurrentTime8+(deprecated)

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: in nanoseconds.
- false: in milliseconds.
callbackAsyncCallback<number>YesCallback used to return the time elapsed since the Unix epoch.

Error codes

For details about the error codes, see Time and Time Zone Service Error Codes.

IDError Message
-1Parameter check failed, permission denied, or system error.

Example

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

try {
  systemTime.getCurrentTime(true, (error: BusinessError, time: number) => {
    if (error) {
      console.info(`Failed to getting 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}`);
}

systemTime.getCurrentTime8+(deprecated)

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.

Error codes

For details about the error codes, see Time and Time Zone Service Error Codes.

IDError Message
-1Parameter check failed, permission denied, or system error.

Example

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

try {
  systemTime.getCurrentTime((error: BusinessError, time: number) => {
    if (error) {
      console.info(`Failed to getting 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}`);
}

systemTime.getCurrentTime8+(deprecated)

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.
The default value is false.
- true: in nanoseconds.
- false: in milliseconds.

Return value

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

Error codes

For details about the error codes, see Time and Time Zone Service Error Codes.

IDError Message
-1Parameter check failed, permission denied, or system error.

Example

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

try {
  systemTime.getCurrentTime().then((time: number) => {
    console.info(`Succeeded in getting currentTime : ${time}`);
  }).catch((error: BusinessError) => {
    console.info(`Failed to getting 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}`);
}

systemTime.getRealActiveTime8+(deprecated)

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: in nanoseconds.
- false: in milliseconds.
callbackAsyncCallback<number>YesCallback used to return the time.

Error codes

For details about the error codes, see Time and Time Zone Service Error Codes.

IDError Message
-1Parameter check failed, permission denied, or system error.

Example

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

try {
  systemTime.getRealActiveTime(true, (error: BusinessError, time: number) => {
    if (error) {
      console.info(`Failed to getting 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}`);
}

systemTime.getRealActiveTime8+(deprecated)

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.

Error codes

For details about the error codes, see Time and Time Zone Service Error Codes.

IDError Message
-1Parameter check failed, permission denied, or system error.

Example

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

try {
  systemTime.getRealActiveTime((error: BusinessError, time: number) => {
    if (error) {
      console.info(`Failed to getting 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}`);
}

systemTime.getRealActiveTime8+(deprecated)

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.
The default value is false.
- true: in nanoseconds.
- false: in milliseconds.

Return value

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

Error codes

For details about the error codes, see Time and Time Zone Service Error Codes.

IDError Message
-1Parameter check failed, permission denied, or system error.

Example

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

try {
  systemTime.getRealActiveTime().then((time: number) => {
    console.info(`Succeeded in getting real active time : ${time}`);
  }).catch((error: BusinessError) => {
    console.info(`Failed to getting 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}`);
}

systemTime.getRealTime8+(deprecated)

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: in nanoseconds.
- false: in milliseconds.
callbackAsyncCallback<number>YesCallback used to return the time.

Error codes

For details about the error codes, see Time and Time Zone Service Error Codes.

IDError Message
-1Parameter check failed, permission denied, or system error.

Example

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

try {
  systemTime.getRealTime(true, (error: BusinessError, time: number) => {
    if (error) {
      console.info(`Failed to getting 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}`);
}

systemTime.getRealTime8+(deprecated)

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.

Error codes

For details about the error codes, see Time and Time Zone Service Error Codes.

IDError Message
-1Parameter check failed, permission denied, or system error.

Example

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

try {
  systemTime.getRealTime((error: BusinessError, time: number) => {
    if (error) {
      console.info(`Failed to getting 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}`);
}

systemTime.getRealTime8+(deprecated)

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.
The default value is false.
- true: in nanoseconds.
- false: in milliseconds.

Return value

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

Error codes

For details about the error codes, see Time and Time Zone Service Error Codes.

IDError Message
-1Parameter check failed, permission denied, or system error.

Example

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

try {
  systemTime.getRealTime().then((time: number) => {
    console.info(`Succeeded in getting real time : ${time}`);
  }).catch((error: BusinessError) => {
    console.info(`Failed to getting 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}`);
}

systemTime.getDate8+(deprecated)

getDate(callback: AsyncCallback<Date>): void

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

System capability: SystemCapability.MiscServices.Time

Parameters

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

Error codes

For details about the error codes, see Time and Time Zone Service Error Codes.

IDError Message
-1Parameter check failed, permission denied, or system error.

Example

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

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

systemTime.getDate8+(deprecated)

getDate(): Promise<Date>

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

System capability: SystemCapability.MiscServices.Time

Return value

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

Error codes

For details about the error codes, see Time and Time Zone Service Error Codes.

IDError Message
-1Parameter check failed, permission denied, or system error.

Example

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

try {
  systemTime.getDate().then((date: Date) => {
    console.info(`Succeeded in getting date : ${date}`);
  }).catch((error: BusinessError) => {
    console.info(`Failed to getting 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}`);
}

systemTime.getTimezone8+(deprecated)

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.

Error codes

For details about the error codes, see Time and Time Zone Service Error Codes.

IDError Message
-1Parameter check failed, permission denied, or system error.

Example

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

try {
  systemTime.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}`);
}

systemTime.getTimezone8+(deprecated)

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.

Error codes

For details about the error codes, see Time and Time Zone Service Error Codes.

IDError Message
-1Parameter check failed, permission denied, or system error.

Example

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

try {
  systemTime.getTimezone().then((data: string) => {
    console.info(`Succeeded in getting timezone: ${data}`);
  }).catch((error: BusinessError) => {
    console.info(`Failed to getting 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}`);
}

systemTime.setTime(deprecated)

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

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

Required permissions: ohos.permission.SET_TIME

System capability: SystemCapability.MiscServices.Time

Parameters

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

Error codes

For details about the error codes, see Time and Time Zone Service Error Codes.

IDError Message
-1Parameter check failed, permission denied, or system error.

Example

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

// Set the system time to 2021-01-20 02:36:25.
let time = 1611081385000;
try {
  systemTime.setTime(time, (error: BusinessError) => {
    if (error) {
      console.info(`Failed to setting 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}`);
}

systemTime.setTime(deprecated)

setTime(time : number) : Promise<void>

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

Required permissions: ohos.permission.SET_TIME

System capability: SystemCapability.MiscServices.Time

Parameters

NameTypeMandatoryDescription
timenumberYesTimestamp to set, in milliseconds.

Return value

TypeDescription
Promise<void>Promise that returns no value.

Error codes

For details about the error codes, see Time and Time Zone Service Error Codes.

IDError Message
-1Parameter check failed, permission denied, or system error.

Example

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

// Set the system time to 2021-01-20 02:36:25.
let time = 1611081385000;
try {
  systemTime.setTime(time).then(() => {
    console.info(`Succeeded in setting time.`);
  }).catch((error: BusinessError) => {
    console.info(`Failed to setting 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}`);
}

systemTime.setDate(deprecated)

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

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

Required permissions: ohos.permission.SET_TIME

System capability: SystemCapability.MiscServices.Time

Parameters

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

Error codes

For details about the error codes, see Time and Time Zone Service Error Codes.

IDError Message
-1Parameter check failed, permission denied, or system error.

Example

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

let date = new Date();
try {
  systemTime.setDate(date, (error: BusinessError) => {
    if (error) {
      console.info(`Failed to setting 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}`);
}

systemTime.setDate(deprecated)

setDate(date: Date): Promise<void>

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

Required permissions: ohos.permission.SET_TIME

System capability: SystemCapability.MiscServices.Time

Parameters

NameTypeMandatoryDescription
dateDateYesTarget date to set.

Return value

TypeDescription
Promise<void>Promise that returns no value.

Error codes

For details about the error codes, see Time and Time Zone Service Error Codes.

IDError Message
-1Parameter check failed, permission denied, or system error.

Example

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

let date = new Date(); 
try {
  systemTime.setDate(date).then(() => {
    console.info(`Succeeded in setting date.`);
  }).catch((error: BusinessError) => {
    console.info(`Failed to setting 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}`);
}

systemTime.setTimezone(deprecated)

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

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

Required permissions: ohos.permission.SET_TIME_ZONE

System capability: SystemCapability.MiscServices.Time

Parameters

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

Error codes

For details about the error codes, see Time and Time Zone Service Error Codes.

IDError Message
-1Parameter check failed, permission denied, or system error.

Example

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

try {
  systemTime.setTimezone('Asia/Shanghai', (error: BusinessError) => {
    if (error) {
      console.info(`Failed to setting 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}`);
}

systemTime.setTimezone(deprecated)

setTimezone(timezone: string): Promise<void>

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

Required permissions: ohos.permission.SET_TIME_ZONE

System capability: SystemCapability.MiscServices.Time

Parameters

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

Return value

TypeDescription
Promise<void>Promise that returns no value.

Error codes

For details about the error codes, see Time and Time Zone Service Error Codes.

IDError Message
-1Parameter check failed, permission denied, or system error.

Example

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

try {
  systemTime.setTimezone('Asia/Shanghai').then(() => {
    console.info(`Succeeded in setting timezone.`);
  }).catch((error: BusinessError) => {
    console.info(`Failed to setting 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}`);
}

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 鸿蒙Basic Services Kit

harmony 鸿蒙DeviceInfo

harmony 鸿蒙InitSync

harmony 鸿蒙OH_Print

harmony 鸿蒙OsAccount

harmony 鸿蒙Pasteboard

harmony 鸿蒙Print_Margin

harmony 鸿蒙Print_PageSize

harmony 鸿蒙Print_PrintAttributes

harmony 鸿蒙Print_PrintDocCallback

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