1# @ohos.systemDateTime (System Time and Time Zone) 2 3The **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. 4 5> **NOTE** 6> 7> 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. 8 9## Modules to Import 10 11```ts 12import systemDateTime from '@ohos.systemDateTime'; 13``` 14 15## TimeType<sup>10+</sup> 16 17Enumerates the types of time to obtain. 18 19**System capability**: SystemCapability.MiscServices.Time 20 21| Name | Value | Description | 22| ------- | ---- | ------------------------------------------------ | 23| STARTUP | 0 | Number of milliseconds elapsed since system startup, including the deep sleep time. | 24| ACTIVE | 1 | Number of milliseconds elapsed since system startup, excluding the deep sleep time.| 25 26## systemDateTime.setTime 27 28setTime(time : number, callback : AsyncCallback<void>) : void 29 30Sets the system time. This API uses an asynchronous callback to return the result. 31 32**System API**: This is a system API. 33 34**System capability**: SystemCapability.MiscServices.Time 35 36**Required permissions**: ohos.permission.SET_TIME 37 38**Parameters** 39 40| Name | Type | Mandatory| Description | 41| -------- | ----------- | ---- | ---------------- | 42| time | number | Yes | Timestamp to set, in milliseconds. | 43| callback | AsyncCallback<void> | Yes | Callback used to return the result.| 44 45**Example** 46 47```ts 48import { BusinessError } from '@ohos.base'; 49 50// Set the system time to 2021-01-20 02:36:25. 51let time = 1611081385000; 52try { 53 systemDateTime.setTime(time, (error: BusinessError) => { 54 if (error) { 55 console.info(`Failed to set time. message: ${error.message}, code: ${error.code}`); 56 return; 57 } 58 console.info(`Succeeded in setting time`); 59 }); 60} catch(e) { 61 let error = e as BusinessError; 62 console.info(`Failed to set time. message: ${error.message}, code: ${error.code}`); 63} 64``` 65 66## systemDateTime.setTime 67 68setTime(time : number) : Promise<void> 69 70Sets the system time. This API uses a promise to return the result. 71 72**System API**: This is a system API. 73 74**System capability**: SystemCapability.MiscServices.Time 75 76**Required permissions**: ohos.permission.SET_TIME 77 78**Parameters** 79 80| Name| Type | Mandatory| Description | 81| ------ | ------ | ---- | ------------------ | 82| time | number | Yes | Timestamp to set, in milliseconds.| 83 84**Return value** 85 86| Type | Description | 87| ------------------- | ------------------------- | 88| Promise<void> | Promise that returns no value.| 89 90**Example** 91 92```ts 93import { BusinessError } from '@ohos.base'; 94 95// Set the system time to 2021-01-20 02:36:25. 96let time = 1611081385000; 97try { 98 systemDateTime.setTime(time).then(() => { 99 console.info(`Succeeded in setting time.`); 100 }).catch((error: BusinessError) => { 101 console.info(`Failed to set time. message: ${error.message}, code: ${error.code}`); 102 }); 103} catch(e) { 104 let error = e as BusinessError; 105 console.info(`Failed to set time. message: ${error.message}, code: ${error.code}`); 106} 107``` 108 109## systemDateTime.getCurrentTime 110 111getCurrentTime(isNano: boolean, callback: AsyncCallback<number>): void 112 113Obtains the time elapsed since the Unix epoch. This API uses an asynchronous callback to return the result. 114 115**System capability**: SystemCapability.MiscServices.Time 116 117**Parameters** 118 119| Name | Type | Mandatory| Description | 120| -------- | -------------- | ---- | ------------------ | 121| 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.| 122| callback | AsyncCallback<number> | Yes | Callback used to return the time elapsed since the Unix epoch. | 123 124**Example** 125 126```ts 127import { BusinessError } from '@ohos.base'; 128 129try { 130 systemDateTime.getCurrentTime(true, (error: BusinessError, time: number) => { 131 if (error) { 132 console.info(`Failed to get currentTime. message: ${error.message}, code: ${error.code}`); 133 return; 134 } 135 console.info(`Succeeded in getting currentTime : ${time}`); 136 }); 137} catch(e) { 138 let error = e as BusinessError; 139 console.info(`Failed to get currentTime. message: ${error.message}, code: ${error.code}`); 140} 141``` 142 143## systemDateTime.getCurrentTime 144 145getCurrentTime(callback: AsyncCallback<number>): void 146 147Obtains the time elapsed since the Unix epoch. This API uses an asynchronous callback to return the result. 148 149**System capability**: SystemCapability.MiscServices.Time 150 151**Parameters** 152 153| Name | Type | Mandatory| Description | 154| -------- | ----------- | ---- | ---------------------------------- | 155| callback | AsyncCallback<number> | Yes | Callback used to return the time elapsed since the Unix epoch, in milliseconds. | 156 157**Example** 158 159```ts 160import { BusinessError } from '@ohos.base'; 161 162try { 163 systemDateTime.getCurrentTime((error: BusinessError, time: number) => { 164 if (error) { 165 console.info(`Failed to get currentTime. message: ${error.message}, code: ${error.code}`); 166 return; 167 } 168 console.info(`Succeeded in getting currentTime : ${time}`); 169 }); 170} catch(e) { 171 let error = e as BusinessError; 172 console.info(`Failed to get currentTime. message: ${error.message}, code: ${error.code}`); 173} 174``` 175 176## systemDateTime.getCurrentTime 177 178getCurrentTime(isNano?: boolean): Promise<number> 179 180Obtains the time elapsed since the Unix epoch. This API uses a promise to return the result. 181 182**System capability**: SystemCapability.MiscServices.Time 183 184**Parameters** 185 186| Name| Type | Mandatory| Description | 187| ------ | ------- | ---- | ------------------------- | 188| 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.| 189 190**Return value** 191 192| Type | Description | 193| --------------------- | --------------------------- | 194| Promise<number> | Promise used to return the time elapsed since the Unix epoch.| 195 196**Example** 197 198```ts 199import { BusinessError } from '@ohos.base'; 200 201try { 202 systemDateTime.getCurrentTime().then((time: number) => { 203 console.info(`Succeeded in getting currentTime : ${time}`); 204 }).catch((error: BusinessError) => { 205 console.info(`Failed to get currentTime. message: ${error.message}, code: ${error.code}`); 206 }); 207} catch(e) { 208 let error = e as BusinessError; 209 console.info(`Failed to get currentTime. message: ${error.message}, code: ${error.code}`); 210} 211``` 212 213## systemDateTime.getRealActiveTime 214 215getRealActiveTime(isNano: boolean, callback: AsyncCallback<number>): void 216 217Obtains the time elapsed since system startup, excluding the deep sleep time. This API uses an asynchronous callback to return the result. 218 219**System capability**: SystemCapability.MiscServices.Time 220 221**Parameters** 222 223| Name | Type | Mandatory| Description | 224| -------- | ---------- | ---- | -------------------------- | 225| 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.| 226| callback | AsyncCallback<number> | Yes | Callback used to return the time.| 227 228**Example** 229 230```ts 231import { BusinessError } from '@ohos.base'; 232 233try { 234 systemDateTime.getRealActiveTime(true, (error: BusinessError, time: number) => { 235 if (error) { 236 console.info(`Failed to get real active time. message: ${error.message}, code: ${error.code}`); 237 return; 238 } 239 console.info(`Succeeded in getting real active time : ${time}`); 240 }); 241} catch(e) { 242 let error = e as BusinessError; 243 console.info(`Failed to get real active time. message: ${error.message}, code: ${error.code}`); 244} 245``` 246 247## systemDateTime.getRealActiveTime 248 249getRealActiveTime(callback: AsyncCallback<number>): void 250 251Obtains the time elapsed since system startup, excluding the deep sleep time. This API uses an asynchronous callback to return the result. 252 253**System capability**: SystemCapability.MiscServices.Time 254 255**Parameters** 256 257| Name | Type | Mandatory| Description | 258| -------- | -------------- | ---- | --------------------- | 259| callback | AsyncCallback<number> | Yes | Callback used to return the time.| 260 261**Example** 262 263```ts 264import { BusinessError } from '@ohos.base'; 265 266try { 267 systemDateTime.getRealActiveTime((error: BusinessError, time: number) => { 268 if (error) { 269 console.info(`Failed to get real active time. message: ${error.message}, code: ${error.code}`); 270 return; 271 } 272 console.info(`Succeeded in getting real active time : ${time}`); 273 }); 274} catch(e) { 275 let error = e as BusinessError; 276 console.info(`Failed to get real active time. message: ${error.message}, code: ${error.code}`); 277} 278``` 279 280## systemDateTime.getRealActiveTime 281 282getRealActiveTime(isNano?: boolean): Promise<number> 283 284Obtains the time elapsed since system startup, excluding the deep sleep time. This API uses a promise to return the result. 285 286**System capability**: SystemCapability.MiscServices.Time 287 288**Parameters** 289 290| Name| Type | Mandatory| Description | 291| ------ | ------- | ---- | ----------------------------------- | 292| 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.| 293 294**Return value** 295 296| Type | Description | 297| -------------- | -------------------------------- | 298| Promise<number> | Promise used to return the time elapsed since system startup, excluding the deep sleep time.| 299 300**Example** 301 302```ts 303import { BusinessError } from '@ohos.base'; 304 305try { 306 systemDateTime.getRealActiveTime().then((time: number) => { 307 console.info(`Succeeded in getting real active time : ${time}`); 308 }).catch((error: BusinessError) => { 309 console.info(`Failed to get real active time. message: ${error.message}, code: ${error.code}`); 310 }); 311} catch(e) { 312 let error = e as BusinessError; 313 console.info(`Failed to get real active time. message: ${error.message}, code: ${error.code}`); 314} 315``` 316 317## systemDateTime.getRealTime 318 319getRealTime(isNano: boolean, callback: AsyncCallback<number>): void 320 321Obtains the time elapsed since system startup, including the deep sleep time. This API uses an asynchronous callback to return the result. 322 323**System capability**: SystemCapability.MiscServices.Time 324 325**Parameters** 326 327| Name | Type | Mandatory| Description | 328| -------- | --------------- | ---- | ------------------------------- | 329| 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.| 330| callback | AsyncCallback<number> | Yes | Callback used to return the time. | 331 332**Example** 333 334```ts 335import { BusinessError } from '@ohos.base'; 336 337try { 338 systemDateTime.getRealTime(true, (error: BusinessError, time: number) => { 339 if (error) { 340 console.info(`Failed to get real time. message: ${error.message}, code: ${error.code}`); 341 return; 342 } 343 console.info(`Succeeded in getting real time : ${time}`); 344 }); 345} catch(e) { 346 let error = e as BusinessError; 347 console.info(`Failed to get real time. message: ${error.message}, code: ${error.code}`); 348} 349``` 350 351## systemDateTime.getRealTime 352 353getRealTime(callback: AsyncCallback<number>): void 354 355Obtains the time elapsed since system startup, including the deep sleep time. This API uses an asynchronous callback to return the result. 356 357**System capability**: SystemCapability.MiscServices.Time 358 359**Parameters** 360 361| Name | Type | Mandatory| Description | 362| -------- | --------- | ---- | --------------------------- | 363| callback | AsyncCallback<number> | Yes | Callback used to return the time. | 364 365**Example** 366 367```ts 368import { BusinessError } from '@ohos.base'; 369 370try { 371 systemDateTime.getRealTime((error: BusinessError, time: number) => { 372 if (error) { 373 console.info(`Failed to get real time. message: ${error.message}, code: ${error.code}`); 374 return; 375 } 376 console.info(`Succeeded in getting real time : ${time}`); 377 }); 378} catch(e) { 379 let error = e as BusinessError; 380 console.info(`Failed to get real time. message: ${error.message}, code: ${error.code}`); 381} 382``` 383 384## systemDateTime.getRealTime 385 386getRealTime(isNano?: boolean): Promise<number> 387 388Obtains the time elapsed since system startup, including the deep sleep time. This API uses a promise to return the result. 389 390**System capability**: SystemCapability.MiscServices.Time 391 392**Parameters** 393 394| Name| Type | Mandatory| Description | 395| ------ | ------- | ---- | ------------------------------- | 396| 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.| 397 398**Return value** 399 400| Type | Description | 401| --------------------- | ------------------------------- | 402| Promise<number> | Promise used to return the time elapsed since system startup, including the deep sleep time.| 403 404**Example** 405 406```ts 407import { BusinessError } from '@ohos.base'; 408 409try { 410 systemDateTime.getRealTime().then((time: number) => { 411 console.info(`Succeeded in getting real time : ${time}`); 412 }).catch((error: BusinessError) => { 413 console.info(`Failed to get real time. message: ${error.message}, code: ${error.code}`); 414 }); 415} catch(e) { 416 let error = e as BusinessError; 417 console.info(`Failed to get real time. message: ${error.message}, code: ${error.code}`); 418} 419``` 420 421## systemDateTime.getTime<sup>10+</sup> 422 423getTime(isNanoseconds?: boolean): number 424 425 Obtains the time elapsed since the Unix epoch. This API returns the result synchronously. 426 427**System capability**: SystemCapability.MiscServices.Time 428 429**Parameters** 430 431| Name | Type | Mandatory| Description | 432| ------------- | ------- | ---- | ------------------------------------------------------------ | 433| 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**| 434 435**Return value** 436 437| Type | Description | 438| ------ | -------------------------- | 439| number | Time elapsed since the Unix epoch.| 440 441**Example** 442 443```ts 444try { 445 let time = systemDateTime.getTime(true) 446} catch(e) { 447 let error = e as BusinessError; 448 console.info(`Failed to get time. message: ${error.message}, code: ${error.code}`); 449} 450``` 451 452## systemDateTime.getUptime<sup>10+</sup> 453 454getUptime(timeType: TimeType, isNanoseconds?: boolean): number 455 456Obtains the time elapsed since system startup. This API returns the result synchronously. 457 458**System capability**: SystemCapability.MiscServices.Time 459 460**Parameters** 461 462| Name | Type | Mandatory| Description | 463| ------------- | ----------------------- | ---- | ------------------------------------------------------------ | 464| timeType | [TimeType](#timetype10) | Yes | Type of the time to obtain. | 465| 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**| 466 467**Return value** 468 469| Type | Description | 470| ------ | -------------------------- | 471| number | Time elapsed since system startup.| 472 473**Example** 474 475```ts 476try { 477 let time = systemDateTime.getUptime(systemDateTime.TimeType.ACTIVE, false); 478} catch(e) { 479 let error = e as BusinessError; 480 console.info(`Failed to get uptime. message: ${error.message}, code: ${error.code}`); 481} 482``` 483 484## systemDateTime.setDate<sup>(deprecated)</sup> 485 486setDate(date: Date, callback: AsyncCallback<void>): void 487 488Sets the system date. This API uses an asynchronous callback to return the result. 489 490> **NOTE** 491> 492> This API is supported since API version 9 and deprecated since API version 10. You are advised to use [systemDateTime.setTime](#systemdatetimesettime) instead. 493 494**System API**: This is a system API. 495 496**System capability**: SystemCapability.MiscServices.Time 497 498**Required permissions**: ohos.permission.SET_TIME 499 500**Parameters** 501 502| Name | Type | Mandatory| Description | 503| -------- | ------------- | ---- | --------------------- | 504| date | Date | Yes | Target date to set. | 505| callback | AsyncCallback<void> | Yes | Callback used to return the result.| 506 507**Example** 508 509```ts 510import { BusinessError } from '@ohos.base'; 511 512let date = new Date(); 513try { 514 systemDateTime.setDate(date, (error: BusinessError) => { 515 if (error) { 516 console.info(`Failed to set date. message: ${error.message}, code: ${error.code}`); 517 return; 518 } 519 console.info(`Succeeded in setting date.`); 520 }); 521} catch(e) { 522 let error = e as BusinessError; 523 console.info(`Failed to set date. message: ${error.message}, code: ${error.code}`); 524} 525``` 526 527## systemDateTime.setDate<sup>(deprecated)</sup> 528 529setDate(date: Date): Promise<void> 530 531Sets the system date. This API uses a promise to return the result. 532 533> **NOTE** 534> 535> This API is supported since API version 9 and deprecated since API version 10. You are advised to use [systemDateTime.setTime](#systemdatetimesettime) instead. 536 537**System API**: This is a system API. 538 539**System capability**: SystemCapability.MiscServices.Time 540 541**Required permissions**: ohos.permission.SET_TIME 542 543**Parameters** 544 545| Name| Type| Mandatory| Description | 546| ------ | ---- | ---- | ---------- | 547| date | Date | Yes | Target date to set.| 548 549**Return value** 550 551| Type | Description | 552| ------------------- | -------------------- | 553| Promise<void> | Promise that returns no value.| 554 555**Example** 556 557```ts 558import { BusinessError } from '@ohos.base'; 559 560let date = new Date(); 561try { 562 systemDateTime.setDate(date).then(() => { 563 console.info(`Succeeded in setting date.`); 564 }).catch((error: BusinessError) => { 565 console.info(`Failed to set date. message: ${error.message}, code: ${error.code}`); 566 }); 567} catch(e) { 568 let error = e as BusinessError; 569 console.info(`Failed to set date. message: ${error.message}, code: ${error.code}`); 570} 571``` 572 573## systemDateTime.getDate<sup>(deprecated)</sup> 574 575getDate(callback: AsyncCallback<Date>): void 576 577Obtains the current system date. This API uses an asynchronous callback to return the result. 578 579> **NOTE** 580> 581> 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. 582 583**System capability**: SystemCapability.MiscServices.Time 584 585**Parameters** 586 587| Name | Type | Mandatory| Description | 588| -------- | -------------- | ---- | --------------------- | 589| callback | AsyncCallback<Date> | Yes | Callback used to return the current system date.| 590 591**Example** 592 593```ts 594import { BusinessError } from '@ohos.base'; 595 596try { 597 systemDateTime.getDate((error: BusinessError, date: Date) => { 598 if (error) { 599 console.info(`Failed to get date. message: ${error.message}, code: ${error.code}`); 600 return; 601 } 602 console.info(`Succeeded in getting date : ${date}`);; 603 }); 604} catch(e) { 605 let error = e as BusinessError; 606 console.info(`Failed to get date. message: ${error.message}, code: ${error.code}`); 607} 608``` 609 610## systemDateTime.getDate<sup>(deprecated)</sup> 611 612getDate(): Promise<Date> 613 614Obtains the current system date. This API uses a promise to return the result. 615 616> **NOTE** 617> 618> 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. 619 620**System capability**: SystemCapability.MiscServices.Time 621 622**Return value** 623 624| Type | Description | 625| ------------------- | ----------------------------------------- | 626| Promise<Date> | Promise used to return the current system date.| 627 628**Example** 629 630```ts 631import { BusinessError } from '@ohos.base'; 632 633try { 634 systemDateTime.getDate().then((date: Date) => { 635 console.info(`Succeeded in getting date : ${date}`); 636 }).catch((error: BusinessError) => { 637 console.info(`Failed to get date. message: ${error.message}, code: ${error.code}`); 638 }); 639} catch(e) { 640 let error = e as BusinessError; 641 console.info(`Failed to get date. message: ${error.message}, code: ${error.code}`); 642} 643``` 644 645## systemDateTime.setTimezone 646 647setTimezone(timezone: string, callback: AsyncCallback<void>): void 648 649Sets the system time zone. This API uses an asynchronous callback to return the result. 650 651**System API**: This is a system API. 652 653**System capability**: SystemCapability.MiscServices.Time 654 655**Required permissions**: ohos.permission.SET_TIME_ZONE 656 657**Parameters** 658 659| Name | Type | Mandatory| Description | 660| -------- | ------------- | ---- | -------------------------- | 661| timezone | string | Yes | System time zone to set. For details, see [Supported System Time Zones](#supported-system-time-zones). | 662| callback | AsyncCallback<void> | Yes | Callback used to return the result.| 663 664**Example** 665 666```ts 667import { BusinessError } from '@ohos.base'; 668 669try { 670 systemDateTime.setTimezone('Asia/Shanghai', (error: BusinessError) => { 671 if (error) { 672 console.info(`Failed to set timezone. message: ${error.message}, code: ${error.code}`); 673 return; 674 } 675 console.info(`Succeeded in setting timezone.`); 676 }); 677} catch(e) { 678 let error = e as BusinessError; 679 console.info(`Failed to set timezone. message: ${error.message}, code: ${error.code}`); 680} 681``` 682 683## systemDateTime.setTimezone 684 685setTimezone(timezone: string): Promise<void> 686 687Sets the system time zone. This API uses a promise to return the result. 688 689**System API**: This is a system API. 690 691**System capability**: SystemCapability.MiscServices.Time 692 693**Required permissions**: ohos.permission.SET_TIME_ZONE 694 695**Parameters** 696 697| Name | Type | Mandatory| Description | 698| -------- | ------ | ---- | ---------- | 699| timezone | string | Yes | System time zone to set. For details, see [Supported System Time Zones](#supported-system-time-zones).| 700 701**Return value** 702 703| Type | Description | 704| ------------------- | -------------------- | 705| Promise<void> | Promise that returns no value.| 706 707**Example** 708 709```ts 710import { BusinessError } from '@ohos.base'; 711 712try { 713 systemDateTime.setTimezone('Asia/Shanghai').then(() => { 714 console.info(`Succeeded in setting timezone.`); 715 }).catch((error: BusinessError) => { 716 console.info(`Failed to set timezone. message: ${error.message}, code: ${error.code}`); 717 }); 718} catch(e) { 719 let error = e as BusinessError; 720 console.info(`Failed to set timezone. message: ${error.message}, code: ${error.code}`); 721} 722``` 723 724## systemDateTime.getTimezone 725 726getTimezone(callback: AsyncCallback<string>): void 727 728Obtains the system time zone. This API uses an asynchronous callback to return the result. 729 730**System capability**: SystemCapability.MiscServices.Time 731 732**Parameters** 733 734| Name | Type | Mandatory| Description | 735| -------- | --------- | ---- | ------------------------ | 736| callback | AsyncCallback<string> | Yes | Callback used to return the system time zone. For details, see [Supported System Time Zones](#supported-system-time-zones).| 737 738**Example** 739 740```ts 741import { BusinessError } from '@ohos.base'; 742 743try { 744 systemDateTime.getTimezone((error: BusinessError, data: string) => { 745 if (error) { 746 console.info(`Failed to get timezone. message: ${error.message}, code: ${error.code}`); 747 return; 748 } 749 console.info(`Succeeded in get timezone : ${data}`);; 750 }); 751} catch(e) { 752 let error = e as BusinessError; 753 console.info(`Failed to get timezone. message: ${error.message}, code: ${error.code}`); 754} 755``` 756 757## systemDateTime.getTimezone 758 759getTimezone(): Promise<string> 760 761Obtains the system time zone. This API uses a promise to return the result. 762 763**System capability**: SystemCapability.MiscServices.Time 764 765**Return value** 766 767| Type | Description | 768| --------------------- | ------------------------------------- | 769| Promise<string> | Promise used to return the system time zone. For details, see [Supported System Time Zones](#supported-system-time-zones).| 770 771**Example** 772 773```ts 774import { BusinessError } from '@ohos.base'; 775 776try { 777 systemDateTime.getTimezone().then((data: string) => { 778 console.info(`Succeeded in getting timezone: ${data}`); 779 }).catch((error: BusinessError) => { 780 console.info(`Failed to get timezone. message: ${error.message}, code: ${error.code}`); 781 }); 782} catch(e) { 783 let error = e as BusinessError; 784 console.info(`Failed to get timezone. message: ${error.message}, code: ${error.code}`); 785} 786``` 787 788## systemDateTime.getTimezoneSync<sup>10+</sup> 789 790getTimezoneSync(): string 791 792Obtain the system time zone. This API returns the result synchronously. 793 794**System capability**: SystemCapability.MiscServices.Time 795 796**Return value** 797 798| Type | Description | 799| ------ | ---------------------------------------------------------- | 800| string | System time zone. For details, see [Supported System Time Zones](#supported-system-time-zones).| 801 802**Example** 803 804```ts 805try { 806 let timezone = systemDateTime.getTimezoneSync(); 807} catch(e) { 808 let error = e as BusinessError; 809 console.info(`Failed to get timezone. message: ${error.message}, code: ${error.code}`); 810} 811``` 812 813## Supported System Time Zones 814 815The following table lists the supported system time zones and the respective offset (unit: h) between each time zone and time zone 0. 816 817| Time Zone | Offset | 818| ------------------------------ | --------------------- | 819| Antarctica/McMurdo | 12 | 820| America/Argentina/Buenos_Aires | -3 | 821| Australia/Sydney | 10 | 822| America/Noronha | -2 | 823| America/St_Johns | -3 | 824| Africa/Kinshasa | 1 | 825| America/Santiago | -3 | 826| Asia/Shanghai | 8 | 827| Asia/Nicosia | 3 | 828| Europe/Berlin | 2 | 829| America/Guayaquil | -5 | 830| Europe/Madrid | 2 | 831| Pacific/Pohnpei | 11 | 832| America/Godthab | -2 | 833| Asia/Jakarta | 7 | 834| Pacific/Tarawa | 12 | 835| Asia/Almaty | 6 | 836| Pacific/Majuro | 12 | 837| Asia/Ulaanbaatar | 8 | 838| America/Mexico_City | -5 | 839| Asia/Kuala_Lumpur | 8 | 840| Pacific/Auckland | 12 | 841| Pacific/Tahiti | -10 | 842| Pacific/Port_Moresby | 10 | 843| Asia/Gaza | 3 | 844| Europe/Lisbon | 1 | 845| Europe/Moscow | 3 | 846| Europe/Kiev | 3 | 847| Pacific/Wake | 12 | 848| America/New_York | -4 | 849| Asia/Tashkent | 5 | 850