js-apis-system-date-time.md 25.8 KB
Newer Older
E
ester.zhou 已提交
1
# @ohos.systemDateTime (System Time and Time Zone)
E
ester.zhou 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14

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

```js
import systemDateTime from '@ohos.systemDateTime';
```

E
ester.zhou 已提交
15 16 17 18 19 20 21 22 23 24 25
## TimeType<sup>10+</sup>

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

E
ester.zhou 已提交
26 27 28 29 30 31 32 33 34 35
## systemDateTime.setTime

setTime(time : number, callback : AsyncCallback&lt;void&gt;) : 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

E
ester.zhou 已提交
36 37
**Required permissions**: ohos.permission.SET_TIME

E
ester.zhou 已提交
38 39 40 41 42 43 44 45 46 47 48 49 50
**Parameters**

| Name  | Type           | Mandatory| Description                                      |
| -------- | ----------- | ---- | ---------------- |
| time     | number                    | Yes  | Timestamp to set, in milliseconds.                        |
| callback | AsyncCallback&lt;void&gt; | Yes  | Callback used to return the result.|

**Example**

```js
// Set the system time to 2021-01-20 02:36:25.
let time = 1611081385000;
try {
E
ester.zhou 已提交
51 52 53 54 55 56 57
  systemDateTime.setTime(time, (error) => {
    if (error) {
      console.info(`Failed to set time. message: ${error.message}, code: ${error.code}`);
      return;
    }
    console.info(`Succeeded in setting time`);
  });
E
ester.zhou 已提交
58
} catch(e) {
E
ester.zhou 已提交
59
  console.info(`Failed to set time. message: ${e.message}, code: ${e.code}`);
E
ester.zhou 已提交
60 61 62 63 64 65 66 67 68 69 70 71 72
}
```

## systemDateTime.setTime

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

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

E
ester.zhou 已提交
73 74
**Required permissions**: ohos.permission.SET_TIME

E
ester.zhou 已提交
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
**Parameters**

| Name| Type  | Mandatory| Description              |
| ------ | ------ | ---- | ------------------ |
| time   | number | Yes  | Timestamp to set, in milliseconds.|

**Return value**

| Type               | Description                     |
| ------------------- | ------------------------- |
| Promise&lt;void&gt; | Promise that returns no value.|

**Example**

```js
// Set the system time to 2021-01-20 02:36:25.
let time = 1611081385000;
try {
E
ester.zhou 已提交
93 94 95 96 97
  systemDateTime.setTime(time).then(() => {
    console.info(`Succeeded in setting time.`);
  }).catch((error) => {
    console.info(`Failed to set time. message: ${error.message}, code: ${error.code}`);
  });
E
ester.zhou 已提交
98
} catch(e) {
E
ester.zhou 已提交
99
  console.info(`Failed to set time. message: ${e.message}, code: ${e.code}`);
E
ester.zhou 已提交
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
}
```

## systemDateTime.getCurrentTime

getCurrentTime(isNano: boolean, callback: AsyncCallback&lt;number&gt;): 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**

| Name  | Type      | Mandatory| Description                            |
| -------- | -------------- | ---- | ------------------ |
| isNano   | boolean                     | Yes  | Whether the time to return is in nanoseconds.<br>- **true**: The time to return is in nanoseconds.<br>- **false**: The time to return is in milliseconds.|
| callback | AsyncCallback&lt;number&gt; | Yes  | Callback used to return the time elapsed since the Unix epoch.        |

**Example**

```js
try {
E
ester.zhou 已提交
122 123 124 125 126 127 128
  systemDateTime.getCurrentTime(true, (error, time) => {
    if (error) {
      console.info(`Failed to get currentTime. message: ${error.message}, code: ${error.code}`);
      return;
    }
    console.info(`Succeeded in getting currentTime : ${time}`);
  });
E
ester.zhou 已提交
129
} catch(e) {
E
ester.zhou 已提交
130
  console.info(`Failed to get currentTime. message: ${e.message}, code: ${e.code}`);
E
ester.zhou 已提交
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
}
```

## systemDateTime.getCurrentTime

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.

**System capability**: SystemCapability.MiscServices.Time

**Parameters**

| Name  | Type              | Mandatory| Description                           |
| -------- | ----------- | ---- | ---------------------------------- |
E
ester.zhou 已提交
146
| callback | AsyncCallback&lt;number&gt; | Yes  | Callback used to return the time elapsed since the Unix epoch, in milliseconds.        |
E
ester.zhou 已提交
147 148 149 150 151

**Example**

```js
try {
E
ester.zhou 已提交
152 153 154 155 156 157 158
  systemDateTime.getCurrentTime((error, time) => {
    if (error) {
      console.info(`Failed to get currentTime. message: ${error.message}, code: ${error.code}`);
      return;
    }
    console.info(`Succeeded in getting currentTime : ${time}`);
  });
E
ester.zhou 已提交
159
} catch(e) {
E
ester.zhou 已提交
160
  console.info(`Failed to get currentTime. message: ${e.message}, code: ${e.code}`);
E
ester.zhou 已提交
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
}
```

## systemDateTime.getCurrentTime

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

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

**System capability**: SystemCapability.MiscServices.Time

**Parameters**

| Name| Type   | Mandatory| Description                    |
| ------ | ------- | ---- | ------------------------- |
E
ester.zhou 已提交
176
| isNano | boolean | No  | Whether the time to return is in nanoseconds. The default value is **false**.<br>- **true**: The time to return is in nanoseconds.<br>- **false**: The time to return is in milliseconds.|
E
ester.zhou 已提交
177 178 179 180 181 182 183 184 185 186 187

**Return value**

| Type       | Description                              |
| --------------------- | --------------------------- |
| Promise&lt;number&gt; | Promise used to return the time elapsed since the Unix epoch.|

**Example**

```js
try {
E
ester.zhou 已提交
188 189 190 191 192
  systemDateTime.getCurrentTime().then((time) => {
    console.info(`Succeeded in getting currentTime : ${time}`);
  }).catch((error) => {
    console.info(`Failed to get currentTime. message: ${error.message}, code: ${error.code}`);
  });
E
ester.zhou 已提交
193
} catch(e) {
E
ester.zhou 已提交
194
  console.info(`Failed to get currentTime. message: ${e.message}, code: ${e.code}`);
E
ester.zhou 已提交
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
}
```

## systemDateTime.getRealActiveTime

getRealActiveTime(isNano: boolean, callback: AsyncCallback&lt;number&gt;): 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**

| Name  | Type                       | Mandatory| Description  |
| -------- | ---------- | ---- | -------------------------- |
| isNano   | boolean                     | Yes  | Whether the time to return is in nanoseconds.<br>- **true**: The time to return is in nanoseconds.<br>- **false**: The time to return is in milliseconds.|
| callback | AsyncCallback&lt;number&gt; | Yes  | Callback used to return the time.|

**Example**

```js
try {
E
ester.zhou 已提交
217 218 219 220 221 222 223
  systemDateTime.getRealActiveTime(true, (error, time) => {
    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}`);
  });
E
ester.zhou 已提交
224
} catch(e) {
E
ester.zhou 已提交
225
  console.info(`Failed to get real active time. message: ${e.message}, code: ${e.code}`);
E
ester.zhou 已提交
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
}
```

## systemDateTime.getRealActiveTime

getRealActiveTime(callback: AsyncCallback&lt;number&gt;): 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**

| Name  | Type                       | Mandatory| Description   |
| -------- | -------------- | ---- | --------------------- |
| callback | AsyncCallback&lt;number&gt; | Yes  | Callback used to return the time.|

**Example**

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

## systemDateTime.getRealActiveTime

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

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

| Name| Type   | Mandatory| Description                             |
| ------ | ------- | ---- | ----------------------------------- |
E
ester.zhou 已提交
271
| isNano | boolean | No  | Whether the time to return is in nanoseconds. The default value is **false**.<br>- **true**: The time to return is in nanoseconds.<br>- **false**: The time to return is in milliseconds.|
E
ester.zhou 已提交
272 273 274 275 276 277 278 279 280 281 282

**Return value**

| Type                 | Description        |
| -------------- | -------------------------------- |
| Promise&lt;number&gt; | Promise used to return the time elapsed since system startup, excluding the deep sleep time.|

**Example**

```js
try {
E
ester.zhou 已提交
283 284 285 286 287
  systemDateTime.getRealActiveTime().then((time) => {
    console.info(`Succeeded in getting real active time : ${time}`);
  }).catch((error) => {
    console.info(`Failed to get real active time. message: ${error.message}, code: ${error.code}`);
  });
E
ester.zhou 已提交
288
} catch(e) {
E
ester.zhou 已提交
289
  console.info(`Failed to get real active time. message: ${e.message}, code: ${e.code}`);
E
ester.zhou 已提交
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
}
```

## systemDateTime.getRealTime

getRealTime(isNano: boolean, callback: AsyncCallback&lt;number&gt;): 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**

| Name  | Type                       | Mandatory| Description  |
| -------- | --------------- | ---- | ------------------------------- |
| isNano   | boolean                     | Yes  | Whether the time to return is in nanoseconds.<br>- **true**: The time to return is in nanoseconds.<br>- **false**: The time to return is in milliseconds.|
| callback | AsyncCallback&lt;number&gt; | Yes  | Callback used to return the time.  |

**Example**

```js
try {
E
ester.zhou 已提交
312 313 314 315 316 317 318
  systemDateTime.getRealTime(true, (error, time) => {
    if (error) {
      console.info(`Failed to get real time. message: ${error.message}, code: ${error.code}`);
      return;
    }
    console.info(`Succeeded in getting real time : ${time}`);
  });
E
ester.zhou 已提交
319
} catch(e) {
E
ester.zhou 已提交
320
  console.info(`Failed to get real time. message: ${e.message}, code: ${e.code}`);
E
ester.zhou 已提交
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
}
```

## systemDateTime.getRealTime

getRealTime(callback: AsyncCallback&lt;number&gt;): 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**

| Name  | Type                       | Mandatory| Description     |
| -------- | --------- | ---- | --------------------------- |
| callback | AsyncCallback&lt;number&gt; | Yes  | Callback used to return the time.  |

**Example**

```js
try {
E
ester.zhou 已提交
342 343 344 345 346 347 348
  systemDateTime.getRealTime((error, time) => {
    if (error) {
      console.info(`Failed to get real time. message: ${error.message}, code: ${error.code}`);
      return;
    }
    console.info(`Succeeded in getting real time : ${time}`);
  });
E
ester.zhou 已提交
349
} catch(e) {
E
ester.zhou 已提交
350
  console.info(`Failed to get real time. message: ${e.message}, code: ${e.code}`);
E
ester.zhou 已提交
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
}
```

## systemDateTime.getRealTime

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

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

| Name| Type   | Mandatory| Description                              |
| ------ | ------- | ---- | ------------------------------- |
E
ester.zhou 已提交
366
| isNano | boolean | No  | Whether the time to return is in nanoseconds. The default value is **false**.<br>- **true**: The time to return is in nanoseconds.<br>- **false**: The time to return is in milliseconds.|
E
ester.zhou 已提交
367 368 369 370 371 372 373 374 375 376 377

**Return value**

| Type                 | Description      |
| --------------------- | ------------------------------- |
| Promise&lt;number&gt; | Promise used to return the time elapsed since system startup, including the deep sleep time.|

**Example**

```js
try {
E
ester.zhou 已提交
378 379 380 381 382
  systemDateTime.getRealTime().then((time) => {
    console.info(`Succeeded in getting real time : ${time}`);
  }).catch((error) => {
    console.info(`Failed to get real time. message: ${error.message}, code: ${error.code}`);
  });
E
ester.zhou 已提交
383
} catch(e) {
E
ester.zhou 已提交
384
  console.info(`Failed to get real time. message: ${e.message}, code: ${e.code}`);
E
ester.zhou 已提交
385 386 387
}
```

E
ester.zhou 已提交
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449
## systemDateTime.getTime<sup>10+</sup>

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.<br>- **true**: The time to return is in nanoseconds.<br>- **false**: The time to return is in milliseconds.<br>Default value: **false**|

**Return value**

| Type  | Description                      |
| ------ | -------------------------- |
| number | Time elapsed since the Unix epoch.|

**Example**

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

## systemDateTime.getUptime<sup>10+</sup>

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](#timetype10) | Yes  | Type of the time to obtain.                                            |
| isNanoseconds | boolean                 | No  | Whether the time to return is in nanoseconds.<br>- **true**: The time to return is in nanoseconds.<br>- **false**: The time to return is in milliseconds.<br>Default value: **false**|

**Return value**

| Type  | Description                      |
| ------ | -------------------------- |
| number | Time elapsed since system startup.|

**Example**

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

## systemDateTime.setDate<sup>(deprecated)</sup>
E
ester.zhou 已提交
450 451 452 453 454

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

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

E
ester.zhou 已提交
455 456 457 458
> **NOTE**
>
> This API is supported since API version 9 and deprecated since API version 10. You are advised to use [systemDateTime.setTime](#systemdatetimesettime) instead.

E
ester.zhou 已提交
459 460 461 462
**System API**: This is a system API.

**System capability**: SystemCapability.MiscServices.Time

E
ester.zhou 已提交
463 464
**Required permissions**: ohos.permission.SET_TIME

E
ester.zhou 已提交
465 466 467 468 469 470 471 472 473 474 475 476
**Parameters**

| Name  | Type                     | Mandatory| Description            |
| -------- | ------------- | ---- | --------------------- |
| date     | Date                      | Yes  | Target date to set.                                |
| callback | AsyncCallback&lt;void&gt; | Yes  | Callback used to return the result.|

**Example**

```js
let date = new Date();
try {
E
ester.zhou 已提交
477 478 479 480 481 482 483
  systemDateTime.setDate(date, (error) => {
    if (error) {
      console.info(`Failed to set date. message: ${error.message}, code: ${error.code}`);
      return;
    }
    console.info(`Succeeded in setting date.`);
  });
E
ester.zhou 已提交
484
} catch(e) {
E
ester.zhou 已提交
485
  console.info(`Failed to set date. message: ${e.message}, code: ${e.code}`);
E
ester.zhou 已提交
486 487 488
}
```

E
ester.zhou 已提交
489
## systemDateTime.setDate<sup>(deprecated)</sup>
E
ester.zhou 已提交
490 491 492 493 494

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

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

E
ester.zhou 已提交
495 496 497 498
> **NOTE**
>
> This API is supported since API version 9 and deprecated since API version 10. You are advised to use [systemDateTime.setTime](#systemdatetimesettime) instead.

E
ester.zhou 已提交
499 500 501 502
**System API**: This is a system API.

**System capability**: SystemCapability.MiscServices.Time

E
ester.zhou 已提交
503 504
**Required permissions**: ohos.permission.SET_TIME

E
ester.zhou 已提交
505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521
**Parameters**

| Name| Type| Mandatory| Description      |
| ------ | ---- | ---- | ---------- |
| date   | Date | Yes  | Target date to set.|

**Return value**

| Type               | Description                |
| ------------------- | -------------------- |
| Promise&lt;void&gt; | Promise that returns no value.|

**Example**

```js
let date = new Date(); 
try {
E
ester.zhou 已提交
522 523 524 525 526
  systemDateTime.setDate(date).then(() => {
    console.info(`Succeeded in setting date.`);
  }).catch((error) => {
    console.info(`Failed to set date. message: ${error.message}, code: ${error.code}`);
  });
E
ester.zhou 已提交
527
} catch(e) {
E
ester.zhou 已提交
528
  console.info(`Failed to set date. message: ${e.message}, code: ${e.code}`);
E
ester.zhou 已提交
529 530 531
}
```

E
ester.zhou 已提交
532
## systemDateTime.getDate<sup>(deprecated)</sup>
E
ester.zhou 已提交
533 534 535 536 537

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 已提交
538 539 540 541
> **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.

E
ester.zhou 已提交
542 543 544 545 546 547 548 549 550 551 552 553
**System capability**: SystemCapability.MiscServices.Time

**Parameters**

| Name  | Type          | Mandatory| Description                  |
| -------- | -------------- | ---- | --------------------- |
| callback | AsyncCallback&lt;Date&gt; | Yes  | Callback used to return the current system date.|

**Example**

```js
try {
E
ester.zhou 已提交
554 555 556 557 558 559 560
  systemDateTime.getDate((error, date) => {
    if (error) {
      console.info(`Failed to get date. message: ${error.message}, code: ${error.code}`);
      return;
    }
    console.info(`Succeeded in getting date : ${date}`);;
  });
E
ester.zhou 已提交
561
} catch(e) {
E
ester.zhou 已提交
562
  console.info(`Failed to get date. message: ${e.message}, code: ${e.code}`);
E
ester.zhou 已提交
563 564 565
}
```

E
ester.zhou 已提交
566
## systemDateTime.getDate<sup>(deprecated)</sup>
E
ester.zhou 已提交
567 568 569 570 571

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

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

E
ester.zhou 已提交
572 573 574 575
> **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.

E
ester.zhou 已提交
576 577 578 579 580 581 582 583 584 585 586 587
**System capability**: SystemCapability.MiscServices.Time

**Return value**

| Type               | Description                                     |
| ------------------- | ----------------------------------------- |
| Promise&lt;Date&gt; | Promise used to return the current system date.|

**Example**

```js
try {
E
ester.zhou 已提交
588 589 590 591 592
  systemDateTime.getDate().then((date) => {
    console.info(`Succeeded in getting date : ${date}`);
  }).catch((error) => {
    console.info(`Failed to get date. message: ${error.message}, code: ${error.code}`);
  });
E
ester.zhou 已提交
593
} catch(e) {
E
ester.zhou 已提交
594
  console.info(`Failed to get date. message: ${e.message}, code: ${e.code}`);
E
ester.zhou 已提交
595 596 597 598 599 600 601 602 603 604 605 606 607
}
```

## systemDateTime.setTimezone

setTimezone(timezone: string, callback: AsyncCallback&lt;void&gt;): 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

E
ester.zhou 已提交
608 609
**Required permissions**: ohos.permission.SET_TIME_ZONE

E
ester.zhou 已提交
610 611 612 613 614 615 616 617 618 619 620
**Parameters**

| Name  | Type             | Mandatory| Description                 |
| -------- | ------------- | ---- | -------------------------- |
| timezone | string                    | Yes  | System time zone to set. For details, see [Supported System Time Zones](#supported-system-time-zones).       |
| callback | AsyncCallback&lt;void&gt; | Yes  | Callback used to return the result.|

**Example**

```js
try {
E
ester.zhou 已提交
621 622 623 624 625 626 627
  systemDateTime.setTimezone('Asia/Shanghai', (error) => {
    if (error) {
      console.info(`Failed to set timezone. message: ${error.message}, code: ${error.code}`);
      return;
    }
    console.info(`Succeeded in setting timezone.`);
  });
E
ester.zhou 已提交
628
} catch(e) {
E
ester.zhou 已提交
629
  console.info(`Failed to set timezone. message: ${e.message}, code: ${e.code}`);
E
ester.zhou 已提交
630 631 632 633 634 635 636 637 638 639 640 641 642
}
```

## systemDateTime.setTimezone

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

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

E
ester.zhou 已提交
643 644
**Required permissions**: ohos.permission.SET_TIME_ZONE

E
ester.zhou 已提交
645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660
**Parameters**

| Name  | Type  | Mandatory| Description      |
| -------- | ------ | ---- | ---------- |
| timezone | string | Yes  | System time zone to set. For details, see [Supported System Time Zones](#supported-system-time-zones).|

**Return value**

| Type               | Description                |
| ------------------- | -------------------- |
| Promise&lt;void&gt; | Promise that returns no value.|

**Example**

```js
try {
E
ester.zhou 已提交
661 662 663 664 665
  systemDateTime.setTimezone('Asia/Shanghai').then(() => {
    console.info(`Succeeded in setting timezone.`);
  }).catch((error) => {
    console.info(`Failed to set timezone. message: ${error.message}, code: ${error.code}`);
  });
E
ester.zhou 已提交
666
} catch(e) {
E
ester.zhou 已提交
667
  console.info(`Failed to set timezone. message: ${e.message}, code: ${e.code}`);
E
ester.zhou 已提交
668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688
}
```

## systemDateTime.getTimezone

getTimezone(callback: AsyncCallback&lt;string&gt;): 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&lt;string&gt; | Yes  | Callback used to return the system time zone. For details, see [Supported System Time Zones](#supported-system-time-zones).|

**Example**

```js
try {
E
ester.zhou 已提交
689 690 691 692 693 694 695
  systemDateTime.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}`);;
  });
E
ester.zhou 已提交
696
} catch(e) {
E
ester.zhou 已提交
697
  console.info(`Failed to get timezone. message: ${e.message}, code: ${e.code}`);
E
ester.zhou 已提交
698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718
}
```

## systemDateTime.getTimezone

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

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

**System capability**: SystemCapability.MiscServices.Time

**Return value**

| Type                 | Description                                 |
| --------------------- | ------------------------------------- |
| Promise&lt;string&gt; | Promise used to return the system time zone. For details, see [Supported System Time Zones](#supported-system-time-zones).|

**Example**

```js
try {
E
ester.zhou 已提交
719 720 721 722 723
  systemDateTime.getTimezone().then((data) => {
    console.info(`Succeeded in getting timezone: ${data}`);
  }).catch((error) => {
    console.info(`Failed to get timezone. message: ${error.message}, code: ${error.code}`);
  });
E
ester.zhou 已提交
724
} catch(e) {
E
ester.zhou 已提交
725
  console.info(`Failed to get timezone. message: ${e.message}, code: ${e.code}`);
E
ester.zhou 已提交
726 727 728
}
```

E
ester.zhou 已提交
729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752
## systemDateTime.getTimezoneSync<sup>10+</sup>

getTimezoneSync(): string

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

**System capability**: SystemCapability.MiscServices.Time

**Return value**

| Type  | Description                                                      |
| ------ | ---------------------------------------------------------- |
| string | System time zone. For details, see [Supported System Time Zones](#supported-system-time-zones).|

**Example**

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

E
ester.zhou 已提交
753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789
## 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                     |