js-apis-system-time.md 24.4 KB
Newer Older
E
ester.zhou 已提交
1
# @ohos.systemTime
Z
zengyawen 已提交
2

E
ester.zhou 已提交
3
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.
Z
zengyawen 已提交
4

E
ester.zhou 已提交
5
> **NOTE**
E
ester.zhou 已提交
6
>
E
ester.zhou 已提交
7
> 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.
Z
zengyawen 已提交
8

W
wusongqing 已提交
9
## Modules to Import
Z
zengyawen 已提交
10

E
ester.zhou 已提交
11
```js
Z
zengyawen 已提交
12 13 14
import systemTime from '@ohos.systemTime';
```

E
ester.zhou 已提交
15
## systemTime.setTime<sup>(deprecated)</sup>
Z
zengyawen 已提交
16

W
wusongqing 已提交
17 18 19 20 21 22
setTime(time : number, callback : AsyncCallback&lt;void&gt;) : void

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

**Required permissions**: ohos.permission.SET_TIME

E
ester.zhou 已提交
23
**System capability**: SystemCapability.MiscServices.Time
W
wusongqing 已提交
24

E
ester.zhou 已提交
25
**Parameters**
W
wusongqing 已提交
26

E
ester.zhou 已提交
27 28 29
| Name  | Type           | Mandatory| Description                                      |
| -------- | ----------- | ---- | ---------------- |
| time     | number                    | Yes  | Timestamp to set, in milliseconds.                        |
E
ester.zhou 已提交
30
| callback | AsyncCallback&lt;void&gt; | Yes  | Callback used to return the result.|
E
ester.zhou 已提交
31 32 33

**Example**

E
ester.zhou 已提交
34 35 36
```js
// Set the system time to 2021-01-20 02:36:25.
let time = 1611081385000;
E
ester.zhou 已提交
37 38 39 40 41 42 43 44 45 46 47
try {
    systemTime.setTime(time, (error) => {
        if (error) {
            console.info(`Failed to setting time. message:${error.message}, code:${error.code}`);
            return;
        }
    	console.info(`Succeeded in setting time`);
	});
} catch(e) {
    console.info(`Failed to set time. message:${e.message}, code:${e.code}`);
}
E
ester.zhou 已提交
48
```
W
wusongqing 已提交
49

E
ester.zhou 已提交
50
## systemTime.setTime<sup>(deprecated)</sup>
W
wusongqing 已提交
51 52 53 54 55 56 57

setTime(time : number) : Promise&lt;void&gt;

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

**Required permissions**: ohos.permission.SET_TIME

E
ester.zhou 已提交
58
**System capability**: SystemCapability.MiscServices.Time
W
wusongqing 已提交
59

E
ester.zhou 已提交
60
**Parameters**
W
wusongqing 已提交
61

E
ester.zhou 已提交
62 63
| Name| Type  | Mandatory| Description              |
| ------ | ------ | ---- | ------------------ |
E
ester.zhou 已提交
64
| time   | number | Yes  | Timestamp to set, in milliseconds.|
W
wusongqing 已提交
65

E
ester.zhou 已提交
66 67
**Return value**

E
ester.zhou 已提交
68 69 70
| Type               | Description                     |
| ------------------- | ------------------------- |
| Promise&lt;void&gt; | Promise that returns no value.|
E
ester.zhou 已提交
71 72 73

**Example**

E
ester.zhou 已提交
74 75 76
```js
// Set the system time to 2021-01-20 02:36:25.
let time = 1611081385000;
E
ester.zhou 已提交
77 78 79 80 81 82 83 84 85
try {
    systemTime.setTime(time).then(() => {
    	console.info(`Succeeded in setting time.`);
	}).catch((error) => {
    	console.info(`Failed to setting time. message:${error.message}, code:${error.code}`);
	});
} catch(e) {
    console.info(`Failed to set time. message:${e.message}, code:${e.code}`);
}
E
ester.zhou 已提交
86
```
W
wusongqing 已提交
87

E
ester.zhou 已提交
88
## systemTime.getCurrentTime<sup>(deprecated)</sup>
W
wusongqing 已提交
89

E
ester.zhou 已提交
90
getCurrentTime(isNano: boolean, callback: AsyncCallback&lt;number&gt;): void
W
wusongqing 已提交
91 92 93

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

E
ester.zhou 已提交
94 95 96 97
>  **NOTE**
>
> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [systemDateTime.getCurrentTime](./js-apis-system-date-time.md#systemdatetimegetcurrenttime).

E
ester.zhou 已提交
98
**System capability**: SystemCapability.MiscServices.Time
W
wusongqing 已提交
99

E
ester.zhou 已提交
100
**Parameters**
W
wusongqing 已提交
101

E
ester.zhou 已提交
102 103 104 105
| Name  | Type      | Mandatory| Description                            |
| -------- | -------------- | ---- | ------------------ |
| isNano   | boolean                     | Yes  | Whether the time to return is in nanoseconds.<br>- **true**: in nanoseconds (ns).<br>- **false**: in milliseconds (ms).|
| callback | AsyncCallback&lt;number&gt; | Yes  | Callback used to return the time elapsed since the Unix epoch.        |
E
ester.zhou 已提交
106 107 108

**Example**

E
ester.zhou 已提交
109
```js
E
ester.zhou 已提交
110 111 112 113 114 115 116 117 118 119 120
try {
    systemTime.getCurrentTime(true, (error, time) => {
        if (error) {
            console.info(`Failed to getting currentTime. message:${error.message}, code:${error.code}`);
            return;
        }
    	console.info(`Succeeded in getting currentTime : ${time}`);
	});
} catch(e) {
    console.info(`Failed to get currentTime. message:${e.message}, code:${e.code}`);
}
E
ester.zhou 已提交
121
```
W
wusongqing 已提交
122

E
ester.zhou 已提交
123
## systemTime.getCurrentTime<sup>(deprecated)</sup>
E
ester.zhou 已提交
124 125 126 127 128

getCurrentTime(callback: AsyncCallback&lt;number&gt;): void

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

E
ester.zhou 已提交
129 130 131 132
> **NOTE**
>
> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [systemDateTime.getCurrentTime](./js-apis-system-date-time.md#systemdatetimegetcurrenttime-1).

E
ester.zhou 已提交
133 134 135 136
**System capability**: SystemCapability.MiscServices.Time

**Parameters**

E
ester.zhou 已提交
137 138 139
| Name  | Type              | Mandatory| Description                           |
| -------- | ----------- | ---- | ---------------------------------- |
| callback | AsyncCallback&lt;number&gt; | Yes  | Callback used to return the time elapsed since the Unix epoch.        |
E
ester.zhou 已提交
140 141 142

**Example**

E
ester.zhou 已提交
143
```js
E
ester.zhou 已提交
144 145 146 147 148 149 150 151 152 153 154
try {
    systemTime.getCurrentTime((error, time) => {
        if (error) {
            console.info(`Failed to getting currentTime. message:${error.message}, code:${error.code}`);
            return;
        }
    	console.info(`Succeeded in getting currentTime : ${time}`);
	});
} catch(e) {
    console.info(`Failed to get currentTime. message:${e.message}, code:${e.code}`);
}
E
ester.zhou 已提交
155
```
E
ester.zhou 已提交
156

E
ester.zhou 已提交
157
## systemTime.getCurrentTime<sup>(deprecated)</sup>
W
wusongqing 已提交
158 159 160 161 162

getCurrentTime(isNano?: boolean): Promise&lt;number&gt;

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

E
ester.zhou 已提交
163 164 165 166
>  **NOTE**
>
> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [systemDateTime.getCurrentTime](./js-apis-system-date-time.md#systemdatetimegetcurrenttime-2).

E
ester.zhou 已提交
167
**System capability**: SystemCapability.MiscServices.Time
W
wusongqing 已提交
168

E
ester.zhou 已提交
169
**Parameters**
W
wusongqing 已提交
170

E
ester.zhou 已提交
171 172 173
| Name| Type   | Mandatory| Description                    |
| ------ | ------- | ---- | ------------------------- |
| isNano | boolean | No  | Whether the time to return is in nanoseconds.<br>- **true**: in nanoseconds (ns).<br>- **false**: in milliseconds (ms).|
W
wusongqing 已提交
174

E
ester.zhou 已提交
175 176
**Return value**

E
ester.zhou 已提交
177 178 179
| Type       | Description                              |
| --------------------- | --------------------------- |
| Promise&lt;number&gt; | Promise used to return the time elapsed since the Unix epoch.|
E
ester.zhou 已提交
180 181 182

**Example**

E
ester.zhou 已提交
183
```js
E
ester.zhou 已提交
184 185 186 187 188 189 190 191 192
try {
    systemTime.getCurrentTime().then((time) => {
    	console.info(`Succeeded in getting currentTime : ${time}`);
	}).catch((error) => {
    	console.info(`Failed to getting currentTime. message:${error.message}, code:${error.code}`);
	});
} catch(e) {
    console.info(`Failed to get currentTime. message:${e.message}, code:${e.code}`);
}
E
ester.zhou 已提交
193
```
W
wusongqing 已提交
194

E
ester.zhou 已提交
195
## systemTime.getRealActiveTime<sup>(deprecated)</sup>
W
wusongqing 已提交
196

E
ester.zhou 已提交
197
getRealActiveTime(isNano: boolean, callback: AsyncCallback&lt;number&gt;): void
W
wusongqing 已提交
198

E
ester.zhou 已提交
199
Obtains the time elapsed since system startup, excluding the deep sleep time. This API uses an asynchronous callback to return the result.
W
wusongqing 已提交
200

E
ester.zhou 已提交
201 202 203 204
> **NOTE**
>
> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [systemDateTime.getRealActiveTime](./js-apis-system-date-time.md#systemdatetimegetrealactivetime).

E
ester.zhou 已提交
205
**System capability**: SystemCapability.MiscServices.Time
W
wusongqing 已提交
206

E
ester.zhou 已提交
207
**Parameters**
W
wusongqing 已提交
208

E
ester.zhou 已提交
209 210 211
| Name  | Type                       | Mandatory| Description  |
| -------- | ---------- | ---- | -------------------------- |
| isNano   | boolean                     | Yes  | Whether the time to return is in nanoseconds.<br>- **true**: in nanoseconds (ns).<br>- **false**: in milliseconds (ms).|
E
ester.zhou 已提交
212
| callback | AsyncCallback&lt;number&gt; | Yes  | Callback used to return the time.|
E
ester.zhou 已提交
213 214 215

**Example**

E
ester.zhou 已提交
216
```js
E
ester.zhou 已提交
217 218 219 220 221 222 223 224 225 226 227
try {
    systemTime.getRealActiveTime(true, (error, time) => {
        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) {
    console.info(`Failed to get real active time. message:${e.message}, code:${e.code}`);
}
E
ester.zhou 已提交
228
```
W
wusongqing 已提交
229

E
ester.zhou 已提交
230
## systemTime.getRealActiveTime<sup>(deprecated)</sup>
E
ester.zhou 已提交
231 232 233

getRealActiveTime(callback: AsyncCallback&lt;number&gt;): void

E
ester.zhou 已提交
234
Obtains the time elapsed since system startup, excluding the deep sleep time. This API uses an asynchronous callback to return the result.
E
ester.zhou 已提交
235

E
ester.zhou 已提交
236 237 238 239
>  **NOTE**
>
> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [systemDateTime.getRealActiveTime](./js-apis-system-date-time.md#systemdatetimegetrealactivetime-1).

E
ester.zhou 已提交
240 241 242 243
**System capability**: SystemCapability.MiscServices.Time

**Parameters**

E
ester.zhou 已提交
244 245
| Name  | Type                       | Mandatory| Description   |
| -------- | -------------- | ---- | --------------------- |
E
ester.zhou 已提交
246 247 248 249
| callback | AsyncCallback&lt;number&gt; | Yes  | Callback used to return the time.|

**Example**

E
ester.zhou 已提交
250
```js
E
ester.zhou 已提交
251 252 253 254 255 256 257 258 259 260 261
try {
    systemTime.getRealActiveTime((error, time) => {
        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) {
    console.info(`Failed to get real active time. message:${e.message}, code:${e.code}`);
}
E
ester.zhou 已提交
262
```
E
ester.zhou 已提交
263

E
ester.zhou 已提交
264
## systemTime.getRealActiveTime<sup>(deprecated)</sup>
W
wusongqing 已提交
265 266 267

getRealActiveTime(isNano?: boolean): Promise&lt;number&gt;

E
ester.zhou 已提交
268
Obtains the time elapsed since system startup, excluding the deep sleep time. This API uses a promise to return the result.
W
wusongqing 已提交
269

E
ester.zhou 已提交
270 271 272 273
>  **NOTE**
>
> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [systemDateTime.getRealActiveTime](./js-apis-system-date-time.md#systemdatetimegetrealactivetime-2).

E
ester.zhou 已提交
274
**System capability**: SystemCapability.MiscServices.Time
W
wusongqing 已提交
275

E
ester.zhou 已提交
276
**Parameters**
W
wusongqing 已提交
277

E
ester.zhou 已提交
278 279 280
| Name| Type   | Mandatory| Description                             |
| ------ | ------- | ---- | ----------------------------------- |
| isNano | boolean | No  | Whether the time to return is in nanoseconds.<br>- **true**: in nanoseconds (ns).<br>- **false**: in milliseconds (ms).|
W
wusongqing 已提交
281

E
ester.zhou 已提交
282 283
**Return value**

E
ester.zhou 已提交
284 285 286
| Type                 | Description        |
| -------------- | -------------------------------- |
| Promise&lt;number&gt; | Promise used to return the time elapsed since system startup, excluding the deep sleep time.|
E
ester.zhou 已提交
287 288 289

**Example**

E
ester.zhou 已提交
290
```js
E
ester.zhou 已提交
291 292 293 294 295 296 297 298 299
try {
    systemTime.getRealActiveTime().then((time) => {
    	console.info(`Succeeded in getting real active time : ${time}`);
	}).catch((error) => {
    	console.info(`Failed to getting real active time. message:${error.message}, code:${error.code}`);
	});
} catch(e) {
    console.info(`Failed to get real active time. message:${e.message}, code:${e.code}`);
}
E
ester.zhou 已提交
300
```
W
wusongqing 已提交
301

E
ester.zhou 已提交
302
## systemTime.getRealTime<sup>(deprecated)</sup>
W
wusongqing 已提交
303

E
ester.zhou 已提交
304
getRealTime(isNano: boolean, callback: AsyncCallback&lt;number&gt;): void
W
wusongqing 已提交
305

E
ester.zhou 已提交
306
Obtains the time elapsed since system startup, including the deep sleep time. This API uses an asynchronous callback to return the result.
W
wusongqing 已提交
307

E
ester.zhou 已提交
308 309 310 311
> **NOTE**
>
> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [systemDateTime.getRealTime](./js-apis-system-date-time.md#systemdatetimegetrealtime).

E
ester.zhou 已提交
312
**System capability**: SystemCapability.MiscServices.Time
W
wusongqing 已提交
313

E
ester.zhou 已提交
314
**Parameters**
W
wusongqing 已提交
315

E
ester.zhou 已提交
316 317 318
| Name  | Type                       | Mandatory| Description  |
| -------- | --------------- | ---- | ------------------------------- |
| isNano   | boolean                     | Yes  | Whether the time to return is in nanoseconds.<br>- **true**: in nanoseconds (ns).<br>- **false**: in milliseconds (ms).|
E
ester.zhou 已提交
319
| callback | AsyncCallback&lt;number&gt; | Yes  | Callback used to return the time.  |
E
ester.zhou 已提交
320 321 322

**Example**

E
ester.zhou 已提交
323
```js
E
ester.zhou 已提交
324 325 326 327 328 329 330 331 332 333 334
try {
    systemTime.getRealTime(true, (error, time) => {
        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) {
    console.info(`Failed to get real time. message:${e.message}, code:${e.code}`);
}
E
ester.zhou 已提交
335
```
W
wusongqing 已提交
336

E
ester.zhou 已提交
337
## systemTime.getRealTime<sup>(deprecated)</sup>
W
wusongqing 已提交
338

E
ester.zhou 已提交
339 340
getRealTime(callback: AsyncCallback&lt;number&gt;): void

E
ester.zhou 已提交
341
Obtains the time elapsed since system startup, including the deep sleep time. This API uses an asynchronous callback to return the result.
E
ester.zhou 已提交
342

E
ester.zhou 已提交
343 344 345 346
>  **NOTE**
>
> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [systemDateTime.getRealTime](./js-apis-system-date-time.md#systemdatetimegetrealtime-1).

E
ester.zhou 已提交
347 348 349 350
**System capability**: SystemCapability.MiscServices.Time

**Parameters**

E
ester.zhou 已提交
351 352
| Name  | Type                       | Mandatory| Description     |
| -------- | --------- | ---- | --------------------------- |
E
ester.zhou 已提交
353 354 355 356
| callback | AsyncCallback&lt;number&gt; | Yes  | Callback used to return the time.  |

**Example**

E
ester.zhou 已提交
357
```js
E
ester.zhou 已提交
358 359 360 361 362 363 364 365 366 367 368
try {
    systemTime.getRealTime((error, time) => {
        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) {
    console.info(`Failed to get real time. message:${e.message}, code:${e.code}`);
}
E
ester.zhou 已提交
369
```
E
ester.zhou 已提交
370

E
ester.zhou 已提交
371
## systemTime.getRealTime<sup>(deprecated)</sup>
E
ester.zhou 已提交
372

E
ester.zhou 已提交
373
getRealTime(isNano?: boolean): Promise&lt;number&gt;
W
wusongqing 已提交
374

E
ester.zhou 已提交
375
Obtains the time elapsed since system startup, including the deep sleep time. This API uses a promise to return the result.
W
wusongqing 已提交
376

E
ester.zhou 已提交
377 378 379 380
> **NOTE**
>
> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [systemDateTime.getRealTime](./js-apis-system-date-time.md#systemdatetimegetrealtime-2).

E
ester.zhou 已提交
381
**System capability**: SystemCapability.MiscServices.Time
W
wusongqing 已提交
382

E
ester.zhou 已提交
383
**Parameters**
W
wusongqing 已提交
384

E
ester.zhou 已提交
385 386
| Name| Type   | Mandatory| Description                              |
| ------ | ------- | ---- | ------------------------------- |
E
ester.zhou 已提交
387
| isNano | boolean | No  | Whether the time to return is in nanoseconds.<br>- **true**: in nanoseconds (ns).<br>- **false**: in milliseconds (ms).|
W
wusongqing 已提交
388

E
ester.zhou 已提交
389 390
**Return value**

E
ester.zhou 已提交
391 392 393
| Type                 | Description      |
| --------------------- | ------------------------------- |
| Promise&lt;number&gt; | Promise used to return the time elapsed since system startup, including the deep sleep time.|
E
ester.zhou 已提交
394 395 396

**Example**

E
ester.zhou 已提交
397
```js
E
ester.zhou 已提交
398 399 400 401 402 403 404 405 406
try {
    systemTime.getRealTime().then((time) => {
    	console.info(`Succeeded in getting real time : ${time}`);
	}).catch((error) => {
    	console.info(`Failed to getting real time. message:${error.message}, code:${error.code}`);
	});
} catch(e) {
    console.info(`Failed to get real time. message:${e.message}, code:${e.code}`);
}
E
ester.zhou 已提交
407
```
W
wusongqing 已提交
408

E
ester.zhou 已提交
409
## systemTime.setDate<sup>(deprecated)</sup>
W
wusongqing 已提交
410 411 412 413 414 415 416

setDate(date: Date, callback: AsyncCallback&lt;void&gt;): void

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

**Required permissions**: ohos.permission.SET_TIME

E
ester.zhou 已提交
417
**System capability**: SystemCapability.MiscServices.Time
W
wusongqing 已提交
418

E
ester.zhou 已提交
419
**Parameters**
W
wusongqing 已提交
420

E
ester.zhou 已提交
421 422
| Name  | Type                     | Mandatory| Description            |
| -------- | ------------- | ---- | --------------------- |
E
ester.zhou 已提交
423 424
| date     | Date                      | Yes  | Target date to set.                                |
| callback | AsyncCallback&lt;void&gt; | Yes  | Callback used to return the result.|
E
ester.zhou 已提交
425 426 427

**Example**

E
ester.zhou 已提交
428
```js
E
ester.zhou 已提交
429 430 431 432 433 434 435 436 437 438 439 440
let date = new Date();
try {
    systemTime.setDate(date, (error) => {
        if (error) {
            console.info(`Failed to setting date. message:${error.message}, code:${error.code}`);
            return;
        }
    	console.info(`Succeeded in setting date.`);
	});
} catch(e) {
    console.info(`Failed to set date. message:${e.message}, code:${e.code}`);
}
E
ester.zhou 已提交
441
```
W
wusongqing 已提交
442

E
ester.zhou 已提交
443
## systemTime.setDate<sup>(deprecated)</sup>
W
wusongqing 已提交
444 445 446 447 448 449 450

setDate(date: Date): Promise&lt;void&gt;

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

**Required permissions**: ohos.permission.SET_TIME

E
ester.zhou 已提交
451
**System capability**: SystemCapability.MiscServices.Time
W
wusongqing 已提交
452

E
ester.zhou 已提交
453
**Parameters**
W
wusongqing 已提交
454

E
ester.zhou 已提交
455 456 457
| Name| Type| Mandatory| Description      |
| ------ | ---- | ---- | ---------- |
| date   | Date | Yes  | Target date to set.|
W
wusongqing 已提交
458

E
ester.zhou 已提交
459 460
**Return value**

E
ester.zhou 已提交
461 462
| Type               | Description                |
| ------------------- | -------------------- |
E
ester.zhou 已提交
463
| Promise&lt;void&gt; | Promise that returns no value.|
E
ester.zhou 已提交
464 465 466

**Example**

E
ester.zhou 已提交
467
```js
E
ester.zhou 已提交
468 469 470 471 472 473 474 475 476 477
let date = new Date(); 
try {
    systemTime.setDate(date).then(() => {
    	console.info(`Succeeded in setting date.`);
	}).catch((error) => {
    	console.info(`Failed to setting date. message:${error.message}, code:${error.code}`);
	});
} catch(e) {
    console.info(`Failed to set date. message:${e.message}, code:${e.code}`);
}
E
ester.zhou 已提交
478
```
W
wusongqing 已提交
479

E
ester.zhou 已提交
480
## systemTime.getDate<sup>(deprecated)</sup>
W
wusongqing 已提交
481 482 483 484 485

getDate(callback: AsyncCallback&lt;Date&gt;): void

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

E
ester.zhou 已提交
486 487 488 489
> **NOTE**
>
> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [systemDateTime.getDate](./js-apis-system-date-time.md#systemdatetimegetdate).

E
ester.zhou 已提交
490
**System capability**: SystemCapability.MiscServices.Time
W
wusongqing 已提交
491

E
ester.zhou 已提交
492
**Parameters**
W
wusongqing 已提交
493

E
ester.zhou 已提交
494 495
| Name  | Type          | Mandatory| Description                  |
| -------- | -------------- | ---- | --------------------- |
E
ester.zhou 已提交
496
| callback | AsyncCallback&lt;Date&gt; | Yes  | Callback used to return the current system date.|
E
ester.zhou 已提交
497 498 499

**Example**

E
ester.zhou 已提交
500
```js
E
ester.zhou 已提交
501 502 503 504 505 506 507 508 509 510 511
try {
    systemTime.getDate((error, 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) {
    console.info(`Failed to get date. message:${e.message}, code:${e.code}`);
}
E
ester.zhou 已提交
512
```
W
wusongqing 已提交
513

E
ester.zhou 已提交
514
## systemTime.getDate<sup>(deprecated)</sup>
W
wusongqing 已提交
515 516 517

getDate(): Promise&lt;Date&gt;

E
ester.zhou 已提交
518
Obtains the current system date. This API uses a promise to return the result.  
W
wusongqing 已提交
519

E
ester.zhou 已提交
520 521 522 523
>  **NOTE**
>
> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [systemDateTime.getDate](./js-apis-system-date-time.md#systemdatetimegetdate-1).

E
ester.zhou 已提交
524
**System capability**: SystemCapability.MiscServices.Time
W
wusongqing 已提交
525

E
ester.zhou 已提交
526
**Return value**
W
wusongqing 已提交
527

E
ester.zhou 已提交
528 529
| Type               | Description                                     |
| ------------------- | ----------------------------------------- |
E
ester.zhou 已提交
530 531 532 533
| Promise&lt;Date&gt; | Promise used to return the current system date.|

**Example**

E
ester.zhou 已提交
534
```js
E
ester.zhou 已提交
535 536 537 538 539 540 541 542 543
try {
    systemTime.getDate().then((date) => {
    	console.info(`Succeeded in getting date : ${date}`);
	}).catch((error) => {
    	console.info(`Failed to getting date. message:${error.message}, code:${error.code}`);
	});
} catch(e) {
    console.info(`Failed to get date. message:${e.message}, code:${e.code}`);
}
E
ester.zhou 已提交
544
```
W
wusongqing 已提交
545

E
ester.zhou 已提交
546
## systemTime.setTimezone<sup>(deprecated)</sup>
W
wusongqing 已提交
547 548 549 550 551 552 553

setTimezone(timezone: string, callback: AsyncCallback&lt;void&gt;): void

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

**Required permissions**: ohos.permission.SET_TIME_ZONE

E
ester.zhou 已提交
554
**System capability**: SystemCapability.MiscServices.Time
W
wusongqing 已提交
555

E
ester.zhou 已提交
556
**Parameters**
W
wusongqing 已提交
557

E
ester.zhou 已提交
558 559
| Name  | Type             | Mandatory| Description                 |
| -------- | ------------- | ---- | -------------------------- |
E
ester.zhou 已提交
560
| timezone | string                    | Yes  | System time zone to set. For details, see [Supported System Time Zones](#supported-system-time-zones).       |
E
ester.zhou 已提交
561
| callback | AsyncCallback&lt;void&gt; | Yes  | Callback used to return the result.|
E
ester.zhou 已提交
562 563 564

**Example**

E
ester.zhou 已提交
565
```js
E
ester.zhou 已提交
566 567 568 569 570 571 572 573 574 575 576
try {
    systemTime.setTimezone('Asia/Shanghai', (error) => {
        if (error) {
            console.info(`Failed to setting timezone. message:${error.message}, code:${error.code}`);
            return;
        }
    	console.info(`Succeeded in setting timezone.`);
	});
} catch(e) {
    console.info(`Failed to set timezone. message:${e.message}, code:${e.code}`);
}
E
ester.zhou 已提交
577
```
W
wusongqing 已提交
578

E
ester.zhou 已提交
579
## systemTime.setTimezone<sup>(deprecated)</sup>
W
wusongqing 已提交
580 581 582 583 584 585 586

setTimezone(timezone: string): Promise&lt;void&gt;

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

**Required permissions**: ohos.permission.SET_TIME_ZONE

E
ester.zhou 已提交
587
**System capability**: SystemCapability.MiscServices.Time
W
wusongqing 已提交
588

E
ester.zhou 已提交
589
**Parameters**
W
wusongqing 已提交
590

E
ester.zhou 已提交
591 592
| Name  | Type  | Mandatory| Description      |
| -------- | ------ | ---- | ---------- |
E
ester.zhou 已提交
593
| timezone | string | Yes  | System time zone to set. For details, see [Supported System Time Zones](#supported-system-time-zones).|
W
wusongqing 已提交
594

E
ester.zhou 已提交
595 596
**Return value**

E
ester.zhou 已提交
597 598
| Type               | Description                |
| ------------------- | -------------------- |
E
ester.zhou 已提交
599
| Promise&lt;void&gt; | Promise that returns no value.|
E
ester.zhou 已提交
600 601 602

**Example**

E
ester.zhou 已提交
603
```js
E
ester.zhou 已提交
604 605 606 607 608 609 610 611 612
try {
    systemTime.setTimezone('Asia/Shanghai').then(() => {
    	console.info(`Succeeded in setting timezone.`);
	}).catch((error) => {
    	console.info(`Failed to setting timezone. message:${error.message}, code:${error.code}`);
	});
} catch(e) {
    console.info(`Failed to set timezone. message:${e.message}, code:${e.code}`);
}
E
ester.zhou 已提交
613
```
W
wusongqing 已提交
614

E
ester.zhou 已提交
615
## systemTime.getTimezone<sup>(deprecated)</sup>
W
wusongqing 已提交
616 617 618 619 620

getTimezone(callback: AsyncCallback&lt;string&gt;): void

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

E
ester.zhou 已提交
621 622 623 624
> **NOTE**
>
> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [systemDateTime.getTimezone](./js-apis-system-date-time.md#systemdatetimegettimezone).

E
ester.zhou 已提交
625
**System capability**: SystemCapability.MiscServices.Time
W
wusongqing 已提交
626

E
ester.zhou 已提交
627
**Parameters**
W
wusongqing 已提交
628

E
ester.zhou 已提交
629 630
| Name  | Type             | Mandatory| Description                |
| -------- | --------- | ---- | ------------------------ |
E
ester.zhou 已提交
631
| callback | AsyncCallback&lt;string&gt; | Yes  | Callback used to return the system time zone. For details, see [Supported System Time Zones](#supported-system-time-zones).|
E
ester.zhou 已提交
632 633 634

**Example**

E
ester.zhou 已提交
635
```js
E
ester.zhou 已提交
636 637 638 639 640 641 642 643 644 645 646
try {
    systemTime.getTimezone((error, data) => {
        if (error) {
            console.info(`Failed to get timezone. message:${error.message}, code:${error.code}`);
            return;
        }
    	console.info(`Succeeded in get timezone : ${data}`);;
	});
} catch(e) {
    console.info(`Failed to get timezone. message:${e.message}, code:${e.code}`);
}
E
ester.zhou 已提交
647
```
W
wusongqing 已提交
648

E
ester.zhou 已提交
649
## systemTime.getTimezone<sup>(deprecated)</sup>
W
wusongqing 已提交
650 651 652 653 654

getTimezone(): Promise&lt;string&gt;

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

E
ester.zhou 已提交
655 656 657 658
> **NOTE**
>
> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [systemDateTime.getTimezone](./js-apis-system-date-time.md#systemdatetimegettimezone-1).

E
ester.zhou 已提交
659
**System capability**: SystemCapability.MiscServices.Time
W
wusongqing 已提交
660

E
ester.zhou 已提交
661
**Return value**
W
wusongqing 已提交
662

E
ester.zhou 已提交
663 664
| Type                 | Description                                 |
| --------------------- | ------------------------------------- |
E
ester.zhou 已提交
665
| Promise&lt;string&gt; | Promise used to return the system time zone. For details, see [Supported System Time Zones](#supported-system-time-zones).|
E
ester.zhou 已提交
666 667 668

**Example**

E
ester.zhou 已提交
669
```js
E
ester.zhou 已提交
670 671 672 673 674 675 676 677 678
try {
    systemTime.getTimezone().then((data) => {
    	console.info(`Succeeded in getting timezone: ${data}`);
	}).catch((error) => {
    	console.info(`Failed to getting timezone. message:${error.message}, code:${error.code}`);
	});
} catch(e) {
    console.info(`Failed to get timezone. message:${e.message}, code:${e.code}`);
}
E
ester.zhou 已提交
679
```
E
ester.zhou 已提交
680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717

## 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                     |