harmony 鸿蒙@ohos.systemDateTime (System Time and Time Zone)
@ohos.systemDateTime (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 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 '@kit.BasicServicesKit';
TimeType10+
Enumerates the types of time to obtain.
System capability: SystemCapability.MiscServices.Time
Name | Value | Description |
---|---|---|
STARTUP | 0 | Number of milliseconds elapsed since system startup, including the deep sleep time. |
ACTIVE | 1 | Number of milliseconds elapsed since system startup, excluding the deep sleep time. |
systemDateTime.getCurrentTime(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.
NOTE
This API is supported since API version 9 and deprecated since API version 12. You are advised to use systemDateTime.getTime10+ instead.
System capability: SystemCapability.MiscServices.Time
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
isNano | boolean | Yes | Whether the time to return is in nanoseconds. - true means that the return result is in nanoseconds (ns). - false means that the return result is in milliseconds (ms). |
callback | AsyncCallback<number> | Yes | Callback used to return the time elapsed since the Unix epoch. |
Error codes
For details about the error codes, see Universal Error Codes.
ID | Error Message |
---|---|
401 | Parameter error. Possible causes: 1.Incorrect parameter types. |
Example
import { BusinessError } from '@kit.BasicServicesKit';
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(deprecated)
getCurrentTime(callback: AsyncCallback<number>): void
Obtains the time elapsed since the Unix epoch. This API uses an asynchronous callback to return the result.
NOTE
This API is supported since API version 9 and deprecated since API version 12. You are advised to use systemDateTime.getTime10+ instead.
System capability: SystemCapability.MiscServices.Time
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
callback | AsyncCallback<number> | Yes | Callback used to return the time elapsed since the Unix epoch, in milliseconds. |
Error codes
For details about the error codes, see Universal Error Codes.
ID | Error Message |
---|---|
401 | Parameter error. Possible causes: 1.Incorrect parameter types. |
Example
import { BusinessError } from '@kit.BasicServicesKit';
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(deprecated)
getCurrentTime(isNano?: boolean): Promise<number>
Obtains the time elapsed since the Unix epoch. This API uses a promise to return the result.
NOTE
This API is supported since API version 9 and deprecated since API version 12. You are advised to use systemDateTime.getTime10+ instead.
System capability: SystemCapability.MiscServices.Time
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
isNano | boolean | No | Whether the time to return is in nanoseconds. The default value is false. - true means that the return result is in nanoseconds (ns). - false means that the return result is in milliseconds (ms). |
Return value
Type | Description |
---|---|
Promise<number> | Promise used to return the timestamp that has elapsed since the Unix epoch. |
Error codes
For details about the error codes, see Universal Error Codes.
ID | Error Message |
---|---|
401 | Parameter error. Possible causes: 1.Incorrect parameter types. |
Example
import { BusinessError } from '@kit.BasicServicesKit';
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(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.
NOTE
This API is supported since API version 9 and deprecated since API version 12. You are advised to use systemDateTime.getUptime10+ instead.
System capability: SystemCapability.MiscServices.Time
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
isNano | boolean | Yes | Whether the time to return is in nanoseconds. - true means that the return result is in nanoseconds (ns). - false means that the return result is in milliseconds (ms). |
callback | AsyncCallback<number> | Yes | Callback used to return the time. |
Error codes
For details about the error codes, see Universal Error Codes.
ID | Error Message |
---|---|
401 | Parameter error. Possible causes: 1.Incorrect parameter types. |
Example
import { BusinessError } from '@kit.BasicServicesKit';
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(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.
NOTE
This API is supported since API version 9 and deprecated since API version 12. You are advised to use systemDateTime.getUptime10+ instead.
System capability: SystemCapability.MiscServices.Time
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
callback | AsyncCallback<number> | Yes | Callback used to return the time. |
Error codes
For details about the error codes, see Universal Error Codes.
ID | Error Message |
---|---|
401 | Parameter error. Possible causes: 1.Incorrect parameter types. |
Example
import { BusinessError } from '@kit.BasicServicesKit';
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(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.
NOTE
This API is supported since API version 9 and deprecated since API version 12. You are advised to use systemDateTime.getUptime10+ instead.
System capability: SystemCapability.MiscServices.Time
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
isNano | boolean | No | Whether the time to return is in nanoseconds. The default value is false. - true means that the return result is in nanoseconds (ns). - false means that the return result is in milliseconds (ms). |
Return value
Type | Description |
---|---|
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 Universal Error Codes.
ID | Error Message |
---|---|
401 | Parameter error. Possible causes: 1.Incorrect parameter types. |
Example
import { BusinessError } from '@kit.BasicServicesKit';
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(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.
NOTE
This API is supported since API version 9 and deprecated since API version 12. You are advised to use systemDateTime.getUptime10+ instead.
System capability: SystemCapability.MiscServices.Time
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
isNano | boolean | Yes | Whether the time to return is in nanoseconds. - true means that the return result is in nanoseconds (ns). - false means that the return result is in milliseconds (ms). |
callback | AsyncCallback<number> | Yes | Callback used to return the time. |
Error codes
For details about the error codes, see Universal Error Codes.
ID | Error Message |
---|---|
401 | Parameter error. Possible causes: 1.Incorrect parameter types. |
Example
import { BusinessError } from '@kit.BasicServicesKit';
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(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.
NOTE
This API is supported since API version 9 and deprecated since API version 12. You are advised to use systemDateTime.getUptime10+ instead.
System capability: SystemCapability.MiscServices.Time
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
callback | AsyncCallback<number> | Yes | Callback used to return the time. |
Error codes
For details about the error codes, see Universal Error Codes.
ID | Error Message |
---|---|
401 | Parameter error. Possible causes: 1.Incorrect parameter types. |
Example
import { BusinessError } from '@kit.BasicServicesKit';
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(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.
NOTE
This API is supported since API version 9 and deprecated since API version 12. You are advised to use systemDateTime.getUptime10+ instead.
System capability: SystemCapability.MiscServices.Time
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
isNano | boolean | No | Whether the time to return is in nanoseconds. The default value is false. - true means that the return result is in nanoseconds (ns). - false means that the return result is in milliseconds (ms). |
Return value
Type | Description |
---|---|
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 Universal Error Codes.
ID | Error Message |
---|---|
401 | Parameter error. Possible causes: 1.Incorrect parameter types. |
Example
import { BusinessError } from '@kit.BasicServicesKit';
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
Name | Type | Mandatory | Description |
---|---|---|---|
isNanoseconds | boolean | No | Whether the time to return is in nanoseconds. - true means that the return result is in nanoseconds (ns). - false means that the return result is in milliseconds (ms). The default value is false. |
Return value
Type | Description |
---|---|
number | Time elapsed since the Unix epoch. |
Example
import { BusinessError } from '@kit.BasicServicesKit';
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
Name | Type | Mandatory | Description |
---|---|---|---|
timeType | TimeType | Yes | Type of the time to be obtained. The value can only be STARTUP or ACTIVE . |
isNanoseconds | boolean | No | Whether the time to return is in nanoseconds. - true means that the return result is in nanoseconds (ns). - false means that the return result is in milliseconds (ms). The default value is false. |
Return value
Type | Description |
---|---|
number | Time elapsed since system startup. |
Error codes
For details about the error codes, see Universal Error Codes.
ID | Error Message |
---|---|
401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types; 3.Parameter verification failed. This error code was added due to missing issues. |
Example
import { BusinessError } from '@kit.BasicServicesKit';
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.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(), which returns the Date object.
System capability: SystemCapability.MiscServices.Time
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
callback | AsyncCallback<Date> | Yes | Callback used to return the current system date. |
Error codes
For details about the error codes, see Universal Error Codes.
ID | Error Message |
---|---|
401 | Parameter error. Possible causes: 1.System error. |
Example
import { BusinessError } from '@kit.BasicServicesKit';
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(), which returns the Date object.
System capability: SystemCapability.MiscServices.Time
Return value
Type | Description |
---|---|
Promise<Date> | Promise used to return the current system date. |
Error codes
For details about the error codes, see Universal Error Codes.
ID | Error Message |
---|---|
401 | Parameter error. Possible causes: 1.System error. |
Example
import { BusinessError } from '@kit.BasicServicesKit';
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.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
Name | Type | Mandatory | Description |
---|---|---|---|
callback | AsyncCallback<string> | Yes | Callback used to return the system time zone. For details, see Supported System Time Zones. |
Example
import { BusinessError } from '@kit.BasicServicesKit';
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
Type | Description |
---|---|
Promise<string> | Promise used to return the system time zone. For details, see Supported System Time Zones. |
Example
import { BusinessError } from '@kit.BasicServicesKit';
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
Obtains the system time zone in synchronous mode.
System capability: SystemCapability.MiscServices.Time
Return value
Type | Description |
---|---|
string | System time zone. For details, see Supported System Time Zones. |
Example
import { BusinessError } from '@kit.BasicServicesKit';
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 Zone | Offset |
---|---|
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 |
你可能感兴趣的鸿蒙文章
- 所属分类: 后端技术
- 本文标签:
热门推荐
-
2、 - 优质文章
-
3、 gate.io
-
8、 golang
-
9、 openharmony
-
10、 Vue中input框自动聚焦