1# @ohos.calendarManager (Calendar Manager) 2 3<!--Kit: Calendar Kit--> 4<!--Subsystem: Applications--> 5<!--Owner: @qq_42718467--> 6<!--Designer: @huangxinwei--> 7<!--Tester: @z30055209--> 8<!--Adviser: @ge-yafang--> 9 10The **calendarManager** module provides APIs for calendar and event management, including those for creating, deleting, modifying, and querying calendars and events. 11 12- A [CalendarManager](#calendarmanager) object is used to manage [Calendar](#calendar) objects. 13 14- A [Calendar](#calendar) object contains the account information [CalendarAccount](#calendaraccount) and configuration information [CalendarConfig](#calendarconfig). An **Event** object is subordinate to a **Calendar** object. To create an **Event** object, you need to create a **Calendar** object first. A **Calendar** contains multiple **Event** objects, but an **Event** belongs to only one **Calendar**. **CalendarManager** is used to manage calendars, and **EventFilter** is used to manage events. 15 16> **NOTE** 17> 18> - The initial APIs of this module are supported since API version 10. Newly added APIs will be marked with a superscript to indicate their earliest API version. 19> 20> - To read a calendar or event, you should request the ohos.permission.READ_CALENDAR or ohos.permission.READ_WHOLE_CALENDAR permission. 21> 22> - To add, delete, or modify a calendar or event, you should request the ohos.permission.WRITE_CALENDAR or ohos.permission.WRITE_WHOLE_CALENDAR permission. 23 24The following table lists the supported operations after the corresponding permission is requested. 25 26| Permission | Operations on the Calendar Account | Operations on the Event | 27| ------------------------------ | -------------------------------------------- | ------------------------------------------------------------ | 28| ohos.permission.READ_CALENDAR | - Read the system calendar account.<br>- Read the calendar account created by the application.| - Read the specified events under the system calendar account.<br>- Read the specified events under the calendar account created by the application.| 29| ohos.permission.WRITE_CALENDAR | - Add, delete, or modify calendar accounts created by the application. | - Add, delete, or modify the specified events under the system calendar account.<br>- Add, delete, or modify the specified events under the calendar account created by the application.| 30| ohos.permission.READ_WHOLE_CALENDAR | - Read all calendar accounts. | - Read all events created by applications. | 31| ohos.permission.WRITE_WHOLE_CALENDAR | - Add, delete, or modify all calendar accounts. | - Add, delete, or modify all events created by applications. | 32 33 34## Modules to Import 35 36```typescript 37import { calendarManager } from '@kit.CalendarKit'; 38``` 39 40## calendarManager.getCalendarManager 41 42getCalendarManager(context: Context): CalendarManager 43 44Obtains a **CalendarManager** object based on the context. 45 46**Atomic service API**: This API can be used in atomic services since API version 11. 47 48**System capability**: SystemCapability.Applications.CalendarData 49 50**Model restriction**: This API can be used only in the stage model. 51 52**Parameters** 53 54| Name | Type | Mandatory| Description | 55| -------- | --------------------------- | ---- |----------------------------------------------------------------------------------------------------------------| 56| context | Context | Yes | Application context. For details about the application context of the stage model, see [Context](../apis-ability-kit/js-apis-inner-application-uiAbilityContext.md).| 57 58**Return value** 59 60| Type | Description | 61| ------------------------------ | ------------------------------------- | 62| CalendarManager | **CalendarManager** object obtained.| 63 64**Example** 65 66```typescript 67// Obtain an mContext object. 68// Obtain a calendarMgr object. 69// The file is auto-generated: entry/src/main/ets/entryability/EntryAbility.ets 70import { 71 abilityAccessCtrl, 72 AbilityConstant, common, PermissionRequestResult, Permissions, UIAbility, Want } from '@kit.AbilityKit'; 73import { BusinessError } from '@kit.BasicServicesKit'; 74import { calendarManager } from '@kit.CalendarKit'; 75import { window } from '@kit.ArkUI'; 76 77export let calendarMgr: calendarManager.CalendarManager | null = null; 78export let mContext: common.UIAbilityContext | null = null; 79export default class EntryAbility extends UIAbility { 80 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { 81 console.info("Ability onCreate"); 82 } 83 84 onDestroy(): void { 85 console.info("Ability onDestroy"); 86 } 87 88 onWindowStageCreate(windowStage: window.WindowStage): void { 89 // Main window is created, set main page for this ability 90 console.info("Ability onWindowStageCreate"); 91 92 windowStage.loadContent('pages/Index', (err, data) => { 93 if (err.code) { 94 console.error(`Failed to load the content. Code: ${err.code}, message: ${err.message}`); 95 return; 96 } 97 console.info(`Succeeded in loading the content. Data: ${JSON.stringify(data)}`); 98 }); 99 mContext = this.context; 100 const permissions: Permissions[] = ['ohos.permission.READ_CALENDAR', 'ohos.permission.WRITE_CALENDAR']; 101 let atManager = abilityAccessCtrl.createAtManager(); 102 atManager.requestPermissionsFromUser(mContext, permissions).then((result: PermissionRequestResult) => { 103 console.info(`get Permission success, result: ${JSON.stringify(result)}`); 104 calendarMgr = calendarManager.getCalendarManager(mContext); 105 }).catch((error: BusinessError) => { 106 console.error(`get Permission error, error. Code: ${error.code}, message: ${error.message}`); 107 }) 108 } 109 110 onWindowStageDestroy(): void { 111 // Main window is destroyed, release UI related resources 112 console.info("Ability onWindowStageDestroy"); 113 } 114 115 onForeground(): void { 116 // Ability has brought to foreground 117 console.info("Ability onForeground"); 118 } 119 120 onBackground(): void { 121 // Ability has back to background 122 console.info("Ability onBackground"); 123 } 124} 125``` 126 127## CalendarManager 128 129Before calling any of the following APIs, you must use [getCalendarManager()](#calendarmanagergetcalendarmanager) to obtain a **CalendarManager** object first. 130 131**System capability**: SystemCapability.Applications.CalendarData 132 133 134### createCalendar 135 136createCalendar(calendarAccount: CalendarAccount, callback: AsyncCallback\<Calendar>): void 137 138Creates a **Calendar** object based on the calendar account information. This API uses an asynchronous callback to return the result. 139 140**Required permissions**: ohos.permission.WRITE_CALENDAR or ohos.permission.WRITE_WHOLE_CALENDAR 141 142**System capability**: SystemCapability.Applications.CalendarData 143 144**Parameters** 145 146| Name | Type | Mandatory| Description | 147| --------------- | ------------------------------------- | ---- | ---------------------------------- | 148| calendarAccount | [CalendarAccount](#calendaraccount) | Yes | Calendar account information. | 149| callback | AsyncCallback\<[Calendar](#calendar)> | Yes | Callback used to return the created **Calendar** object.| 150 151**Error codes** 152 153For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 154 155| ID| Error Message | 156| -------- | ------------------------------ | 157| 201 | Permission denied. | 158| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 159| 801 | Capability not supported. | 160 161**Example** 162 163```typescript 164import { BusinessError } from '@kit.BasicServicesKit'; 165import { calendarMgr } from '../entryability/EntryAbility'; 166 167let calendar: calendarManager.Calendar | undefined = undefined; 168const calendarAccount: calendarManager.CalendarAccount = { 169 name: 'CreateMyCalendarByCallBack', 170 type: calendarManager.CalendarType.LOCAL 171}; 172try { 173 calendarMgr?.createCalendar(calendarAccount, (err: BusinessError, data: calendarManager.Calendar) => { 174 if (err) { 175 console.error(`Failed to create calendar. Code: ${err.code}, message: ${err.message}`); 176 } else { 177 console.info(`Succeeded in creating calendar, data -> ${JSON.stringify(data)}`); 178 calendar = data; 179 } 180 }); 181} catch (error) { 182 console.error(`Failed to create calendar. Code: ${error.code}, message: ${error.message}`); 183} 184``` 185 186### createCalendar 187 188createCalendar(calendarAccount: CalendarAccount): Promise\<Calendar> 189 190Creates a **Calendar** object based on the calendar account information. This API uses a promise to return the result. 191 192**Required permissions**: ohos.permission.WRITE_CALENDAR or ohos.permission.WRITE_WHOLE_CALENDAR 193 194**System capability**: SystemCapability.Applications.CalendarData 195 196**Parameters** 197 198| Name | Type | Mandatory| Description | 199| --------------- | ----------------------------------- | ---- | -------------- | 200| calendarAccount | [CalendarAccount](#calendaraccount) | Yes | Calendar account information.| 201 202**Return value** 203 204| Type | Description | 205| ------------------------------ | ------------------------------------- | 206| Promise<[Calendar](#calendar)> | Promise used to return the created **Calendar** object.| 207 208**Error codes** 209 210For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 211 212| ID| Error Message | 213| -------- | ------------------------------ | 214| 201 | Permission denied. | 215| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 216| 801 | Capability not supported. | 217 218**Example** 219 220```typescript 221import { BusinessError } from '@kit.BasicServicesKit'; 222import { calendarMgr } from '../entryability/EntryAbility'; 223 224let calendar : calendarManager.Calendar | undefined = undefined; 225const calendarAccount: calendarManager.CalendarAccount = { 226 name: 'CreateMyCalendarByPromise', 227 type: calendarManager.CalendarType.LOCAL, 228 displayName : 'MyApplication' 229}; 230calendarMgr?.createCalendar(calendarAccount).then((data: calendarManager.Calendar) => { 231 console.info(`Succeeded in creating calendar data->${JSON.stringify(data)}`); 232 calendar = data; 233}).catch((error : BusinessError) => { 234 console.error(`Failed to create calendar. Code: ${error.code}, message: ${error.message}`); 235}); 236``` 237 238### deleteCalendar 239 240deleteCalendar(calendar: Calendar, callback: AsyncCallback\<void>): void 241 242Deletes a specified **Calendar** object. This API uses an asynchronous callback to return the result. 243 244**Required permissions**: ohos.permission.WRITE_CALENDAR or ohos.permission.WRITE_WHOLE_CALENDAR 245 246**System capability**: SystemCapability.Applications.CalendarData 247 248**Parameters** 249 250| Name | Type | Mandatory| Description | 251| -------- | --------------------- | ---- | -------------- | 252| calendar | [Calendar](#calendar) | Yes | **Calendar** object to delete.| 253| callback | AsyncCallback\<void> | Yes | Asynchronous callback that returns no value. | 254 255**Error codes** 256 257For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 258 259| ID| Error Message | 260| -------- | ------------------------------ | 261| 201 | Permission denied. | 262| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 263| 801 | Capability not supported. | 264 265**Example** 266 267```typescript 268import { BusinessError } from '@kit.BasicServicesKit'; 269import { calendarMgr } from '../entryability/EntryAbility'; 270 271const calendarAccount: calendarManager.CalendarAccount = { 272 name: 'DeleteMyCalendarByCallBack', 273 type: calendarManager.CalendarType.LOCAL 274}; 275calendarMgr?.createCalendar(calendarAccount).then((data: calendarManager.Calendar) => { 276 console.info(`Succeeded in creating calendar, data -> ${JSON.stringify(data)}`); 277 calendarMgr?.getCalendar(calendarAccount, (err: BusinessError, data: calendarManager.Calendar) => { 278 if (err) { 279 console.error(`Failed to get calendar. Code: ${err.code}, message: ${err.message}`); 280 } else { 281 console.info(`Succeeded in getting calendar, data -> ${JSON.stringify(data)}`); 282 calendarMgr?.deleteCalendar(data, (err1: BusinessError) => { 283 if (err1) { 284 console.error(`Failed to delete calendar. Code: ${err1.code}, message: ${err1.message}`); 285 } else { 286 console.info("Succeeded in deleting calendar"); 287 } 288 }); 289 } 290 }); 291}).catch((error: BusinessError) => { 292 console.error(`Failed to create calendar. Code: ${error.code}, message: ${error.message}`); 293}) 294``` 295 296### deleteCalendar 297 298deleteCalendar(calendar: Calendar): Promise\<void> 299 300Deletes a specified **Calendar** object. This API uses a promise to return the result. 301 302**Required permissions**: ohos.permission.WRITE_CALENDAR or ohos.permission.WRITE_WHOLE_CALENDAR 303 304**System capability**: SystemCapability.Applications.CalendarData 305 306**Parameters** 307 308| Name | Type | Mandatory| Description | 309| -------- | --------------------- | ---- | -------------- | 310| calendar | [Calendar](#calendar) | Yes | **Calendar** object to delete.| 311 312**Return value** 313 314| Type | Description | 315| -------------- | ------------------------- | 316| Promise\<void> | Promise that returns no value.| 317 318**Error codes** 319 320For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 321 322| ID| Error Message | 323| -------- | ------------------------------ | 324| 201 | Permission denied. | 325| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 326| 801 | Capability not supported. | 327 328**Example** 329 330```typescript 331import { BusinessError } from '@kit.BasicServicesKit'; 332import { calendarMgr } from '../entryability/EntryAbility'; 333 334const calendarAccount: calendarManager.CalendarAccount = { 335 name: 'DeleteMyCalendarByPromise', 336 type: calendarManager.CalendarType.LOCAL 337}; 338calendarMgr?.createCalendar(calendarAccount).then((data: calendarManager.Calendar) => { 339 console.info(`Succeeded in creating calendar, data -> ${JSON.stringify(data)}`); 340 calendarMgr?.getCalendar(calendarAccount).then((data: calendarManager.Calendar) => { 341 console.info(`Succeeded in getting calendar, data -> ${JSON.stringify(data)}`); 342 calendarMgr?.deleteCalendar(data).then(() => { 343 console.info("Succeeded in deleting calendar"); 344 }).catch((err: BusinessError) => { 345 console.error(`Failed to delete calendar. Code: ${err.code}, message: ${err.message}`); 346 }); 347 }).catch((err: BusinessError) => { 348 console.error(`Failed to get calendar. Code: ${err.code}, message: ${err.message}`); 349 }); 350}).catch((error: BusinessError) => { 351 console.error(`Failed to create calendar. Code: ${error.code}, message: ${error.message}`); 352}) 353``` 354 355### getCalendar 356 357getCalendar(callback: AsyncCallback\<Calendar>): void 358 359Obtains the default **Calendar** object, which is created when the data storage runs for the first time. This API uses an asynchronous callback to return the result. You can call this API instead of [createCalendar()](#createcalendar) to use the default calendar for a new event. 360 361**Required permissions**: ohos.permission.READ_CALENDAR or ohos.permission.READ_WHOLE_CALENDAR 362 363**Atomic service API**: This API can be used in atomic services since API version 11. 364 365**System capability**: SystemCapability.Applications.CalendarData 366 367**Parameters** 368 369| Name | Type | Mandatory| Description | 370| -------- | ------------------------------------ | ---- | ------------------------------------ | 371| callback | AsyncCallback<[Calendar](#calendar)> | Yes | Callback used to return the obtained **Calendar** object.| 372 373**Error codes** 374 375For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 376 377| ID| Error Message | 378| -------- | ------------------------------ | 379| 201 | Permission denied. | 380| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 381| 801 | Capability not supported. | 382 383**Example** 384 385```typescript 386import { BusinessError } from '@kit.BasicServicesKit'; 387import { calendarMgr } from '../entryability/EntryAbility'; 388 389let calendar : calendarManager.Calendar | undefined = undefined; 390calendarMgr?.getCalendar((err: BusinessError, data:calendarManager.Calendar) => { 391 if (err) { 392 console.error(`Failed to get calendar. Code: ${err.code}, message: ${err.message}`); 393 } else { 394 console.info(`Succeeded in getting calendar, data -> ${JSON.stringify(data)}`); 395 calendar = data; 396 } 397}); 398``` 399 400### getCalendar 401 402getCalendar(calendarAccount: CalendarAccount, callback: AsyncCallback\<Calendar>): void 403 404Obtains a specified **Calendar** object. This API uses an asynchronous callback to return the result. 405 406**Required permissions**: ohos.permission.READ_CALENDAR or ohos.permission.READ_WHOLE_CALENDAR 407 408**Atomic service API**: This API can be used in atomic services since API version 11. 409 410**System capability**: SystemCapability.Applications.CalendarData 411 412**Parameters** 413 414| Name | Type | Mandatory| Description | 415| --------------- | ------------------------------------ | ---- | ------------------------------------ | 416| calendarAccount | [CalendarAccount](#calendaraccount) | Yes | Calendar account information. | 417| callback | AsyncCallback<[Calendar](#calendar)> | Yes | Callback used to return the obtained **Calendar** object.| 418 419**Error codes** 420 421For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 422 423| ID| Error Message | 424| -------- | ------------------------------ | 425| 201 | Permission denied. | 426| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 427| 801 | Capability not supported. | 428 429**Example** 430 431```typescript 432import { BusinessError } from '@kit.BasicServicesKit'; 433import { calendarMgr } from '../entryability/EntryAbility'; 434 435let calendar : calendarManager.Calendar | undefined = undefined; 436const calendarAccount: calendarManager.CalendarAccount = { 437 name: 'MyCalendar', 438 type: calendarManager.CalendarType.LOCAL 439}; 440calendarMgr?.createCalendar(calendarAccount).then((data: calendarManager.Calendar) => { 441 console.info(`Succeeded in creating calendar, data -> ${JSON.stringify(data)}`); 442 calendarMgr?.getCalendar(calendarAccount, (err: BusinessError, data: calendarManager.Calendar) => { 443 if (err) { 444 console.error(`Failed to get calendar. Code: ${err.code}, message: ${err.message}`); 445 } else { 446 console.info(`Succeeded in getting calendar data -> ${JSON.stringify(data)}`); 447 calendar = data; 448 } 449 }); 450}).catch((error: BusinessError) => { 451 console.error(`Failed to create calendar. Code: ${error.code}, message: ${error.message}`); 452}) 453``` 454 455### getCalendar 456 457getCalendar(calendarAccount?: CalendarAccount): Promise\<Calendar> 458 459Obtains the default **Calendar** object or a specified **Calendar** object. This API uses a promise to return the result. 460 461**Required permissions**: ohos.permission.READ_CALENDAR or ohos.permission.READ_WHOLE_CALENDAR 462 463**Atomic service API**: This API can be used in atomic services since API version 11. 464 465**System capability**: SystemCapability.Applications.CalendarData 466 467**Parameters** 468 469| Name | Type | Mandatory| Description | 470| --------------- | ----------------------------------- | ---- | ------------------------------------------------------------ | 471| calendarAccount | [CalendarAccount](#calendaraccount) | No | Calendar account information, which is used to obtain a specified **Calendar** object. If this parameter is not set, the default **Calendar** object is obtained.| 472 473**Return value** 474 475| Type | Description | 476| ------------------------------ | --------------------------------------- | 477| Promise<[Calendar](#calendar)> | Promise used to return the obtained **Calendar** object.| 478 479**Error codes** 480 481For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 482 483| ID| Error Message | 484| -------- | ------------------------------ | 485| 201 | Permission denied. | 486| 401 | Parameter error. Possible causes: Incorrect parameter types. | 487| 801 | Capability not supported. | 488 489**Example** 490 491```typescript 492import { BusinessError } from '@kit.BasicServicesKit'; 493import { calendarMgr } from '../entryability/EntryAbility'; 494 495let calendar : calendarManager.Calendar | undefined = undefined; 496calendarMgr?.getCalendar().then((data: calendarManager.Calendar) => { 497 console.info(`Succeeded in getting calendar, data -> ${JSON.stringify(data)}`); 498 calendar = data; 499}).catch((err: BusinessError) => { 500 console.error(`Failed to get calendar. Code: ${err.code}, message: ${err.message}`); 501}); 502``` 503 504### getAllCalendars 505 506getAllCalendars(callback: AsyncCallback\<Calendar[]>): void 507 508Obtains the created and default **Calendar** objects of the current application. This API uses an asynchronous callback to return the result. 509 510**Required permissions**: ohos.permission.READ_CALENDAR or ohos.permission.READ_WHOLE_CALENDAR 511 512**System capability**: SystemCapability.Applications.CalendarData 513 514**Parameters** 515 516| Name | Type | Mandatory| Description | 517| -------- | -------------------------------------- | ---- | ----------------------------------------- | 518| callback | AsyncCallback<[Calendar](#calendar)[]> | Yes | Callback used to return an array of the obtained **Calendar** objects.| 519 520**Error codes** 521 522For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 523 524| ID| Error Message | 525| -------- | ------------------------------ | 526| 201 | Permission denied. | 527| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 528| 801 | Capability not supported. | 529 530**Example** 531 532```typescript 533import { BusinessError } from '@kit.BasicServicesKit'; 534import { calendarMgr } from '../entryability/EntryAbility'; 535 536calendarMgr?.getAllCalendars((err: BusinessError, data: calendarManager.Calendar[]) => { 537 if (err) { 538 console.error(`Failed to get all calendars. Code: ${err.code}, message: ${err.message}`); 539 } else { 540 console.info(`Succeeded in getting all calendars, data -> ${JSON.stringify(data)}`); 541 data.forEach((calendar) => { 542 const account = calendar.getAccount(); 543 console.info(`account -> ${JSON.stringify(account)}`); 544 }) 545 } 546}); 547``` 548 549### getAllCalendars 550 551getAllCalendars(): Promise\<Calendar[]> 552 553Obtains the created and default **Calendar** objects of the current application. This API uses a promise to return the result. 554 555**Required permissions**: ohos.permission.READ_CALENDAR or ohos.permission.READ_WHOLE_CALENDAR 556 557**System capability**: SystemCapability.Applications.CalendarData 558 559**Return value** 560 561| Type | Description | 562| -------------------------------- | ------------------------------------------- | 563| Promise<[Calendar](#calendar)[]> | Promise used to return an array of obtained **Calendar** objects.| 564 565**Error codes** 566 567For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 568 569| ID| Error Message | 570| -------- | ------------------------------ | 571| 201 | Permission denied. | 572| 401 | Parameter error. Possible causes: Incorrect parameter types. | 573| 801 | Capability not supported. | 574 575**Example** 576 577```typescript 578import { BusinessError } from '@kit.BasicServicesKit'; 579import { calendarMgr } from '../entryability/EntryAbility'; 580 581calendarMgr?.getAllCalendars().then((data: calendarManager.Calendar[]) => { 582 console.info(`Succeeded in getting all calendars, data -> ${JSON.stringify(data)}`); 583 data.forEach((calendar) => { 584 const account = calendar.getAccount(); 585 console.info(`account -> ${JSON.stringify(account)}`); 586 }) 587}).catch((err: BusinessError) => { 588 console.error(`Failed to get all calendars. Code: ${err.code}, message: ${err.message}`); 589}); 590``` 591 592### editEvent<sup>12+</sup> 593 594editEvent(event: Event): Promise\<number> 595 596Edits an event on the event creation page, with no event ID specified in **Event**. This API uses a promise to return the result. Events created using this API cannot be queried or modified by third-party applications, but only by the system calendar. 597 598**Atomic service API**: This API can be used in atomic services since API version 12. 599 600**System capability**: SystemCapability.Applications.CalendarData 601 602**Parameters** 603 604| Name| Type | Mandatory| Description | 605| ------ | --------------- | ---- | ----------- | 606| event | [Event](#event) | Yes | **Event** object.| 607 608**Return value** 609 610| Type | Description | 611| -------------- |-----------------------------------------------------------------------------| 612| Promise<number> | Promise used to return the event ID. The event ID is the unique identifier of an event and is the auto-increment primary key of the database. If the event creation fails, no value is returned; if the value is less than **0**, the event creation is canceled; if the value is greater than **0**, the event creation is successful. The return value cannot be **0**| 613 614**Example** 615 616```typescript 617import { BusinessError } from '@kit.BasicServicesKit'; 618import { calendarMgr } from '../entryability/EntryAbility'; 619 620const date = new Date(); 621const event: calendarManager.Event = { 622 title: 'title', 623 type: calendarManager.EventType.NORMAL, 624 startTime: date.getTime(), 625 endTime: date.getTime() + 60 * 60 * 1000 626}; 627calendarMgr?.editEvent(event).then((eventId: number): void => { 628 console.info(`create Event id = ${eventId}`); 629}).catch((err: BusinessError) => { 630 console.error(`Failed to create Event. Code: ${err.code}, message: ${err.message}`); 631}); 632``` 633 634## Calendar 635 636In the following API examples, you need to use [createCalendar()](#createcalendar) or [getCalendar()](#getcalendar) to obtain a **Calendar** object before calling related APIs. 637 638**System capability**: SystemCapability.Applications.CalendarData 639 640### Attributes 641 642**Atomic service API**: This API can be used in atomic services since API version 11. 643 644**System capability**: SystemCapability.Applications.CalendarData 645 646| Name| Type | Read Only| Optional| Description | 647| ---- | ------ | ---- |----|--------------------------------------------------------------------------| 648| id | number | Yes | No | Calendar account ID, which is the unique identifier of a calendar account and is the auto-increment primary key of the database. If the value is less than **0**, the account creation fails; if the value is greater than **0**, the account creation succeeds.| 649 650### addEvent 651 652addEvent(event: Event, callback: AsyncCallback\<number>): void 653 654Adds an event, with no event ID, **instanceStartTime**, and **instanceEndTime** specified in **Event**. This API uses an asynchronous callback to return the result. 655 656**Atomic service API**: This API can be used in atomic services since API version 11. 657 658**System capability**: SystemCapability.Applications.CalendarData 659 660**Parameters** 661 662| Name | Type | Mandatory| Description | 663| -------- | ---------------------- | ---- |-----------------------------------------------------------------------| 664| event | [Event](#event) | Yes | **Event** object. | 665| callback | AsyncCallback\<number> | Yes | Callback used to return the event ID. The event ID is the unique identifier of an event and is the auto-increment primary key of the database. If the value is less than **0**, the event creation fails; if the value is greater than **0**, the event creation succeeds.| 666 667**Example** 668 669```typescript 670import { BusinessError } from '@kit.BasicServicesKit'; 671import { calendarMgr } from '../entryability/EntryAbility'; 672 673let calendar : calendarManager.Calendar | undefined = undefined; 674const date = new Date(); 675const event: calendarManager.Event = { 676 type: calendarManager.EventType.NORMAL, 677 startTime: date.getTime(), 678 endTime: date.getTime() + 60 * 60 * 1000 679}; 680calendarMgr?.getCalendar().then((data: calendarManager.Calendar) => { 681 console.info(`Succeeded in getting calendar, data -> ${JSON.stringify(data)}`); 682 calendar = data; 683 calendar.addEvent(event, (err: BusinessError, data: number): void => { 684 if (err) { 685 console.error(`Failed to addEvent. Code: ${err.code}, message: ${err.message}`); 686 } else { 687 console.info(`Succeeded in adding event, id -> ${data}`); 688 } 689 }); 690}).catch((err: BusinessError) => { 691 console.error(`Failed to get calendar. Code: ${err.code}, message: ${err.message}`); 692}); 693``` 694 695### addEvent 696 697addEvent(event: Event): Promise\<number> 698 699Adds an event, with no event ID, **instanceStartTime**, and **instanceEndTime** specified in **Event**. This API uses a promise to return the result. 700 701**Atomic service API**: This API can be used in atomic services since API version 11. 702 703**System capability**: SystemCapability.Applications.CalendarData 704 705**Parameters** 706 707| Name| Type | Mandatory| Description | 708| ------ | --------------- | ---- | ----------- | 709| event | [Event](#event) | Yes | **Event** object.| 710 711**Return value** 712 713| Type | Description | 714| ---------------- |--------------------------| 715| Promise\<number> | Promise used to return the event ID. The ID is greater than 0. | 716 717**Example** 718 719```typescript 720import { BusinessError } from '@kit.BasicServicesKit'; 721import { calendarMgr } from '../entryability/EntryAbility'; 722 723let calendar : calendarManager.Calendar | undefined = undefined; 724const date = new Date(); 725const event: calendarManager.Event = { 726 type: calendarManager.EventType.NORMAL, 727 startTime: date.getTime(), 728 endTime: date.getTime() + 60 * 60 * 1000 729}; 730calendarMgr?.getCalendar((err: BusinessError, data:calendarManager.Calendar) => { 731 if (err) { 732 console.error(`Failed to get calendar. Code: ${err.code}, message: ${err.message}`); 733 } else { 734 console.info(`Succeeded in getting calendar, data -> ${JSON.stringify(data)}`); 735 calendar = data; 736 calendar.addEvent(event).then((data: number) => { 737 console.info(`Succeeded in adding event, id -> ${data}`); 738 }).catch((err: BusinessError) => { 739 console.error(`Failed to addEvent. Code: ${err.code}, message: ${err.message}`); 740 }); 741 } 742}); 743``` 744 745### addEvents 746 747addEvents(events: Event[], callback: AsyncCallback\<void>): void 748 749Adds events in batches, with no event ID, **instanceStartTime**, and **instanceEndTime** specified in **Event**. This API uses an asynchronous callback to return the result. 750 751**System capability**: SystemCapability.Applications.CalendarData 752 753**Parameters** 754 755| Name | Type | Mandatory| Description | 756| -------- | -------------------- | ---- | --------------- | 757| events | [Event](#event)[] | Yes | Array of **Event** objects.| 758| callback | AsyncCallback\<void> | Yes | Callback used to return the result. | 759 760**Example** 761 762```typescript 763import { BusinessError } from '@kit.BasicServicesKit'; 764import { calendarMgr } from '../entryability/EntryAbility'; 765 766let calendar : calendarManager.Calendar | undefined = undefined; 767const date = new Date(); 768const events: calendarManager.Event[] = [ 769 { 770 type: calendarManager.EventType.NORMAL, 771 startTime: date.getTime(), 772 endTime: date.getTime() + 60 * 60 * 1000 773 }, 774 { 775 type: calendarManager.EventType.NORMAL, 776 startTime: date.getTime(), 777 endTime: date.getTime() + 60 * 60 * 1000 778 } 779]; 780calendarMgr?.getCalendar((err: BusinessError, data:calendarManager.Calendar) => { 781 if (err) { 782 console.error(`Failed to get calendar. Code: ${err.code}, message: ${err.message}`); 783 } else { 784 console.info(`Succeeded in getting calendar, data -> ${JSON.stringify(data)}`); 785 calendar = data; 786 calendar.addEvents(events, (err: BusinessError) => { 787 if (err) { 788 console.error(`Failed to add events. Code: ${err.code}, message: ${err.message}`); 789 } else { 790 console.info("Succeeded in adding events"); 791 } 792 }); 793 } 794}); 795``` 796 797### addEvents 798 799addEvents(events: Event[]): Promise\<void> 800 801Adds events in batches, with no event ID, **instanceStartTime**, and **instanceEndTime** specified in **Event**. This API uses a promise to return the result. 802 803**System capability**: SystemCapability.Applications.CalendarData 804 805**Parameters** 806 807| Name| Type | Mandatory| Description | 808| ------ | ----------------- | ---- | --------------- | 809| events | [Event](#event)[] | Yes | Array of **Event** objects.| 810 811**Return value** 812 813| Type | Description | 814| -------------- | ------------------------- | 815| Promise\<void> | Promise that returns no value.| 816 817**Example** 818 819```typescript 820import { BusinessError } from '@kit.BasicServicesKit'; 821import { calendarMgr } from '../entryability/EntryAbility'; 822 823let calendar : calendarManager.Calendar | undefined = undefined; 824const date = new Date(); 825const events: calendarManager.Event[] = [ 826 { 827 type: calendarManager.EventType.NORMAL, 828 startTime: date.getTime(), 829 endTime: date.getTime() + 60 * 60 * 1000 830 }, 831 { 832 type: calendarManager.EventType.NORMAL, 833 startTime: date.getTime(), 834 endTime: date.getTime() + 60 * 60 * 1000 835 } 836]; 837calendarMgr?.getCalendar((err: BusinessError, data:calendarManager.Calendar) => { 838 if (err) { 839 console.error(`Failed to get calendar. Code: ${err.code}, message: ${err.message}`); 840 } else { 841 console.info(`Succeeded in getting calendar, data -> ${JSON.stringify(data)}`); 842 calendar = data; 843 calendar.addEvents(events).then(() => { 844 console.info("Succeeded in adding events"); 845 }).catch((err: BusinessError) => { 846 console.error(`Failed to add event. Code: ${err.code}, message: ${err.message}`); 847 }); 848 } 849}); 850``` 851 852### deleteEvent 853 854deleteEvent(id: number, callback: AsyncCallback\<void>): void 855 856Deletes an event with the specified ID. This API uses an asynchronous callback to return the result. 857 858**System capability**: SystemCapability.Applications.CalendarData 859 860**Parameters** 861 862| Name | Type | Mandatory| Description | 863| -------- | -------------------- | ---- |----------------------------------------| 864| id | number | Yes | Event ID, which is the unique identifier of an event. If the input event ID is a positive integer, the event is created.| 865| callback | AsyncCallback\<void> | Yes | Callback used to return the result. | 866 867**Example** 868 869```typescript 870import { BusinessError } from '@kit.BasicServicesKit'; 871import { calendarMgr } from '../entryability/EntryAbility'; 872 873let calendar : calendarManager.Calendar | undefined = undefined; 874let id: number = 0; 875const date = new Date(); 876const event: calendarManager.Event = { 877 type: calendarManager.EventType.NORMAL, 878 startTime: date.getTime(), 879 endTime: date.getTime() + 60 * 60 * 1000 880}; 881calendarMgr?.getCalendar(async (err: BusinessError, data:calendarManager.Calendar) => { 882 if (err) { 883 console.error(`Failed to get calendar. Code: ${err.code}, message: ${err.message}`); 884 } else { 885 console.info(`Succeeded in getting calendar, data -> ${JSON.stringify(data)}`); 886 calendar = data; 887 calendar.addEvent(event).then((data: number) => { 888 console.info(`Succeeded in adding event, id -> ${data}`); 889 id = data; 890 calendar?.deleteEvent(id, (err: BusinessError) => { 891 if (err) { 892 console.error(`Failed to delete event. Code: ${err.code}, message: ${err.message}`); 893 } else { 894 console.info(`Succeeded in deleting event`); 895 } 896 }); 897 }).catch((err: BusinessError) => { 898 console.error(`Failed to add event. Code: ${err.code}, message: ${err.message}`); 899 }); 900 } 901}); 902``` 903 904### deleteEvent 905 906deleteEvent(id: number): Promise\<void> 907 908Deletes an event with the specified ID. This API uses a promise to return the result. 909 910**System capability**: SystemCapability.Applications.CalendarData 911 912**Parameters** 913 914| Name| Type | Mandatory| Description | 915| ------ | ------ | ---- | -------- | 916| id | number | Yes | Event ID.| 917 918**Return value** 919 920| Type | Description | 921| -------------- | ------------------------- | 922| Promise\<void> | Promise that returns no value.| 923 924**Example** 925 926```typescript 927import { BusinessError } from '@kit.BasicServicesKit'; 928import { calendarMgr } from '../entryability/EntryAbility'; 929 930let calendar : calendarManager.Calendar | undefined = undefined; 931let id: number = 0; 932const date = new Date(); 933const event: calendarManager.Event = { 934 type: calendarManager.EventType.NORMAL, 935 startTime: date.getTime(), 936 endTime: date.getTime() + 60 * 60 * 1000 937}; 938calendarMgr?.getCalendar(async (err: BusinessError, data:calendarManager.Calendar) => { 939 if (err) { 940 console.error(`Failed to get calendar. Code: ${err.code}, message: ${err.message}`); 941 } else { 942 console.info(`Succeeded in getting calendar data->${JSON.stringify(data)}`); 943 calendar = data; 944 await calendar.addEvent(event).then((data: number) => { 945 console.info(`Succeeded in adding event, id -> ${data}`); 946 id = data; 947 }).catch((err: BusinessError) => { 948 console.error(`Failed to add event. Code: ${err.code}, message: ${err.message}`); 949 }); 950 calendar.deleteEvent(id).then(() => { 951 console.info("Succeeded in deleting event"); 952 }).catch((err: BusinessError) => { 953 console.error(`Failed to delete event. Code: ${err.code}, message: ${err.message}`); 954 }); 955 } 956}); 957``` 958 959### deleteEvents 960 961deleteEvents(ids: number[], callback: AsyncCallback\<void>): void 962 963Deletes a batch of events with the specified IDs. This API uses an asynchronous callback to return the result. 964 965**System capability**: SystemCapability.Applications.CalendarData 966 967**Parameters** 968 969| Name | Type | Mandatory| Description | 970| -------- | -------------------- | ---- | ------------ | 971| ids | number[] | Yes | Array of event IDs.| 972| callback | AsyncCallback\<void> | Yes | Callback used to return the result. | 973 974**Example** 975 976```typescript 977import { BusinessError } from '@kit.BasicServicesKit'; 978import { calendarMgr } from '../entryability/EntryAbility'; 979 980let calendar : calendarManager.Calendar | undefined = undefined; 981let id1: number = 0; 982let id2: number = 0; 983const date = new Date(); 984const event1: calendarManager.Event = { 985 type: calendarManager.EventType.NORMAL, 986 startTime: date.getTime(), 987 endTime: date.getTime() + 60 * 60 * 1000 988}; 989const event2: calendarManager.Event = { 990 type: calendarManager.EventType.IMPORTANT, 991 startTime: date.getTime(), 992 endTime: date.getTime() + 60 * 60 * 1000 993}; 994calendarMgr?.getCalendar(async (err: BusinessError, data:calendarManager.Calendar) => { 995 if (err) { 996 console.error(`Failed to get calendar. Code: ${err.code}, message: ${err.message}`); 997 } else { 998 console.info(`Succeeded in getting calendar, data -> ${JSON.stringify(data)}`); 999 calendar = data; 1000 await calendar.addEvent(event1).then((data: number) => { 1001 console.info(`Succeeded in adding event, id -> ${data}`); 1002 id1 = data; 1003 }).catch((err: BusinessError) => { 1004 console.error(`Failed to add event. Code: ${err.code}, message: ${err.message}`); 1005 }); 1006 await calendar.addEvent(event2).then((data: number) => { 1007 console.info(`Succeeded in adding event, id -> ${data}`); 1008 id2 = data; 1009 }).catch((err: BusinessError) => { 1010 console.error(`Failed to add event. Code: ${err.code}, message: ${err.message}`); 1011 }); 1012 calendar.deleteEvents([id1, id2], (err: BusinessError) => { 1013 if (err) { 1014 console.error(`Failed to delete events. Code: ${err.code}, message: ${err.message}`); 1015 } else { 1016 console.info("Succeeded in deleting events"); 1017 } 1018 }); 1019 } 1020}); 1021``` 1022 1023### deleteEvents 1024 1025deleteEvents(ids: number[]): Promise\<void> 1026 1027Deletes a batch of events with the specified IDs. This API uses a promise to return the result. 1028 1029**System capability**: SystemCapability.Applications.CalendarData 1030 1031**Parameters** 1032 1033| Name| Type | Mandatory| Description | 1034| ------ | -------- | ---- | ------------ | 1035| ids | number[] | Yes | Array of event IDs.| 1036 1037**Return value** 1038 1039| Type | Description | 1040| -------------- | ------------------------- | 1041| Promise\<void> | Promise that returns no value.| 1042 1043**Example** 1044 1045```typescript 1046import { BusinessError } from '@kit.BasicServicesKit'; 1047import { calendarMgr } from '../entryability/EntryAbility'; 1048 1049let calendar : calendarManager.Calendar | undefined = undefined; 1050let id1: number = 0; 1051let id2: number = 0; 1052const date = new Date(); 1053const event1: calendarManager.Event = { 1054 type: calendarManager.EventType.NORMAL, 1055 startTime: date.getTime(), 1056 endTime: date.getTime() + 60 * 60 * 1000 1057}; 1058const event2: calendarManager.Event = { 1059 type: calendarManager.EventType.IMPORTANT, 1060 startTime: date.getTime(), 1061 endTime: date.getTime() + 60 * 60 * 1000 1062}; 1063calendarMgr?.getCalendar(async (err: BusinessError, data:calendarManager.Calendar) => { 1064 if (err) { 1065 console.error(`Failed to get calendar. Code: ${err.code}, message: ${err.message}`); 1066 } else { 1067 console.info(`Succeeded in getting calendar, data -> ${JSON.stringify(data)}`); 1068 calendar = data; 1069 await calendar.addEvent(event1).then((data: number) => { 1070 console.info(`Succeeded in adding event, id -> ${data}`); 1071 id1 = data; 1072 }).catch((err: BusinessError) => { 1073 console.error(`Failed to add event. Code: ${err.code}, message: ${err.message}`); 1074 }); 1075 await calendar.addEvent(event2).then((data: number) => { 1076 console.info(`Succeeded in adding event, id -> ${data}`); 1077 id2 = data; 1078 }).catch((err: BusinessError) => { 1079 console.error(`Failed to add event. Code: ${err.code}, message: ${err.message}`); 1080 }); 1081 calendar.deleteEvents([id1, id2]).then(() => { 1082 console.info("Succeeded in deleting events"); 1083 }).catch((err: BusinessError) => { 1084 console.error(`Failed to delete events. Code: ${err.code}, message: ${err.message}`); 1085 }); 1086 } 1087}); 1088``` 1089 1090### updateEvent 1091 1092updateEvent(event: Event, callback: AsyncCallback\<void>): void 1093 1094Updates an event. This API uses an asynchronous callback to return the result. 1095 1096**System capability**: SystemCapability.Applications.CalendarData 1097 1098**Parameters** 1099 1100| Name | Type | Mandatory| Description | 1101| -------- | -------------------- | ---- | ----------- | 1102| event | [Event](#event) | Yes | **Event** object.| 1103| callback | AsyncCallback\<void> | Yes | Callback used to return the result. | 1104 1105**Example** 1106 1107```typescript 1108import { BusinessError } from '@kit.BasicServicesKit'; 1109import { calendarMgr } from '../entryability/EntryAbility'; 1110 1111let calendar : calendarManager.Calendar | undefined = undefined; 1112const date = new Date(); 1113const oriEvent: calendarManager.Event = { 1114 title: 'update', 1115 type: calendarManager.EventType.NORMAL, 1116 description: 'updateEventTest', 1117 startTime: date.getTime(), 1118 endTime: date.getTime() + 60 * 60 * 1000 1119}; 1120calendarMgr?.getCalendar(async (err: BusinessError, data:calendarManager.Calendar) => { 1121 if (err) { 1122 console.error(`Failed to get calendar. Code: ${err.code}, message: ${err.message}`); 1123 } else { 1124 console.info(`Succeeded in getting calendar, data -> ${JSON.stringify(data)}`); 1125 calendar = data; 1126 await calendar.addEvent(oriEvent).then((data: number) => { 1127 console.info(`Succeeded in adding event, id -> ${data}`); 1128 oriEvent.id = data; 1129 oriEvent.title = 'newUpdate'; 1130 }).catch((err: BusinessError) => { 1131 console.error(`Failed to add event. Code: ${err.code}, message: ${err.message}`); 1132 }); 1133 calendar.updateEvent(oriEvent, (err: BusinessError) => { 1134 if (err) { 1135 console.error(`Failed to update event. Code: ${err.code}, message: ${err.message}`); 1136 } else { 1137 console.info("Succeeded in updating event"); 1138 } 1139 }); 1140 } 1141}); 1142``` 1143 1144### updateEvent 1145 1146updateEvent(event: Event): Promise\<void> 1147 1148Updates an event. This API uses a promise to return the result. 1149 1150**System capability**: SystemCapability.Applications.CalendarData 1151 1152**Parameters** 1153 1154| Name| Type | Mandatory| Description | 1155| ------ | --------------- | ---- | ----------- | 1156| event | [Event](#event) | Yes | **Event** object.| 1157 1158**Return value** 1159 1160| Type | Description | 1161| -------------- | ------------------------- | 1162| Promise\<void> | Promise that returns no value.| 1163 1164**Example** 1165 1166```typescript 1167import { BusinessError } from '@kit.BasicServicesKit'; 1168import { calendarMgr } from '../entryability/EntryAbility'; 1169 1170let calendar : calendarManager.Calendar | undefined = undefined; 1171const date = new Date(); 1172const oriEvent: calendarManager.Event = { 1173 title: 'update', 1174 type: calendarManager.EventType.NORMAL, 1175 description: 'updateEventTest', 1176 startTime: date.getTime(), 1177 endTime: date.getTime() + 60 * 60 * 1000 1178}; 1179calendarMgr?.getCalendar(async (err: BusinessError, data:calendarManager.Calendar) => { 1180 if (err) { 1181 console.error(`Failed to get calendar. Code: ${err.code}, message: ${err.message}`); 1182 } else { 1183 console.info(`Succeeded in getting calendar, data -> ${JSON.stringify(data)}`); 1184 calendar = data; 1185 await calendar.addEvent(oriEvent).then((data: number) => { 1186 console.info(`Succeeded in adding event, id -> ${data}`); 1187 oriEvent.id = data; 1188 oriEvent.title = 'newUpdate'; 1189 }).catch((err: BusinessError) => { 1190 console.error(`Failed to add event. Code: ${err.code}, message: ${err.message}`); 1191 }); 1192 calendar.updateEvent(oriEvent).then(() => { 1193 console.info(`Succeeded in updating event`); 1194 }).catch((err: BusinessError) => { 1195 console.error(`Failed to update event. Code: ${err.code}, message: ${err.message}`); 1196 }); 1197 } 1198}); 1199``` 1200 1201### getEvents 1202 1203getEvents(callback: AsyncCallback\<Event[]>): void 1204 1205Obtains all events in the current calendar. This API uses an asynchronous callback to return the result. 1206 1207**System capability**: SystemCapability.Applications.CalendarData 1208 1209**Parameters** 1210 1211| Name | Type | Mandatory| Description | 1212| -------- | -------------------------------- | ---- | --------------------------------- | 1213| callback | AsyncCallback<[Event](#event)[]> | Yes | Callback used to return an array of events.| 1214 1215**Example** 1216 1217```typescript 1218import { BusinessError } from '@kit.BasicServicesKit'; 1219import { calendarMgr } from '../entryability/EntryAbility'; 1220 1221let calendar : calendarManager.Calendar | undefined = undefined; 1222calendarMgr?.getCalendar((err: BusinessError, data:calendarManager.Calendar) => { 1223 if (err) { 1224 console.error(`Failed to get calendar. Code: ${err.code}, message: ${err.message}`); 1225 } else { 1226 console.info(`Succeeded in getting calendar data -> ${JSON.stringify(data)}`); 1227 calendar = data; 1228 calendar.getEvents((err: BusinessError, data: calendarManager.Event[]) => { 1229 if (err) { 1230 console.error(`Failed to get events. Code: ${err.code}, message: ${err.message}`); 1231 } else { 1232 console.info(`Succeeded in getting events, data -> ${JSON.stringify(data)}`); 1233 } 1234 }); 1235 } 1236}); 1237``` 1238 1239### getEvents 1240 1241getEvents(eventFilter: EventFilter, eventKey: (keyof Event)[], callback: AsyncCallback\<Event[]>):void 1242 1243Obtains all events in a calendar that match the filter criteria. This API uses an asynchronous callback to return the result. 1244 1245**System capability**: SystemCapability.Applications.CalendarData 1246 1247**Parameters** 1248 1249| Name | Type | Mandatory| Description | 1250| ----------- | -------------------------------- | ---- | --------------------------------- | 1251| eventFilter | [EventFilter](#eventfilter) | Yes | Filter criteria. | 1252| eventKey | (keyof [Event](#event))[] | Yes | Filter field. | 1253| callback | AsyncCallback<[Event](#event)[]> | Yes | Callback used to return an array of events.| 1254 1255**Example** 1256 1257```typescript 1258import { BusinessError } from '@kit.BasicServicesKit'; 1259import { calendarMgr } from '../entryability/EntryAbility'; 1260 1261let calendar : calendarManager.Calendar | undefined = undefined; 1262let id1: number = 0; 1263let id2: number = 0; 1264const date = new Date(); 1265const event1: calendarManager.Event = { 1266 type: calendarManager.EventType.NORMAL, 1267 startTime: date.getTime(), 1268 endTime: date.getTime() + 60 * 60 * 1000 1269}; 1270const event2: calendarManager.Event = { 1271 type: calendarManager.EventType.IMPORTANT, 1272 startTime: date.getTime(), 1273 endTime: date.getTime() + 60 * 60 * 1000 1274}; 1275calendarMgr?.getCalendar(async (err: BusinessError, data:calendarManager.Calendar) => { 1276 if (err) { 1277 console.error(`Failed to get calendar. Code: ${err.code}, message: ${err.message}`); 1278 } else { 1279 console.info(`Succeeded in getting calendar, data -> ${JSON.stringify(data)}`); 1280 calendar = data; 1281 await calendar.addEvent(event1).then((data: number) => { 1282 console.info(`Succeeded in adding event, id -> ${data}`); 1283 }).catch((err: BusinessError) => { 1284 console.error(`Failed to add event. Code: ${err.code}, message: ${err.message}`); 1285 }); 1286 await calendar.addEvent(event2).then((data: number) => { 1287 console.info(`Succeeded in adding event, id -> ${data}`); 1288 }).catch((err: BusinessError) => { 1289 console.error(`Failed to add event. Code: ${err.code}, message: ${err.message}`); 1290 }); 1291 const filter = calendarManager.EventFilter.filterById([id1, id2]); 1292 calendar.getEvents(filter, ['title', 'type', 'startTime', 'endTime'], (err: BusinessError, data: calendarManager.Event[]) => { 1293 if (err) { 1294 console.error(`Failed to get events. Code: ${err.code}, message: ${err.message}`); 1295 } else { 1296 console.info(`Succeeded in getting events, data -> ${JSON.stringify(data)}`); 1297 } 1298 }); 1299 } 1300}); 1301``` 1302 1303### getEvents 1304 1305getEvents(eventFilter?: EventFilter, eventKey?: (keyof Event)[]): Promise\<Event[]> 1306 1307Obtains all events in a calendar that match the filter criteria. This API uses a promise to return the result. 1308If there is only one input parameter, the filter criteria, corresponding to the type **EventFilter**, must be set as the parameter. 1309If no input parameter is specified, all events under the specified calendar account can be queried. 1310 1311**System capability**: SystemCapability.Applications.CalendarData 1312 1313**Parameters** 1314 1315| Name | Type | Mandatory| Description | 1316| ----------- | --------------------------- | ---- | ---------- | 1317| eventFilter | [EventFilter](#eventfilter) | No | Filter criteria.| 1318| eventKey | (keyof [Event](#event))[] | No | Filter field.| 1319 1320**Return value** 1321 1322| Type | Description | 1323| -------------------------- | ----------------------------------- | 1324| Promise<[Event](#event)[]> | Promise used to return the result, which is an array of **Event** objects.| 1325 1326**Example** 1327 1328```typescript 1329import { BusinessError } from '@kit.BasicServicesKit'; 1330import { calendarMgr } from '../entryability/EntryAbility'; 1331 1332let calendar : calendarManager.Calendar | undefined = undefined; 1333const date = new Date(); 1334const event: calendarManager.Event = { 1335 title: 'MyEvent', 1336 type: calendarManager.EventType.IMPORTANT, 1337 startTime: date.getTime(), 1338 endTime: date.getTime() + 60 * 60 * 1000 1339}; 1340calendarMgr?.getCalendar(async (err: BusinessError, data:calendarManager.Calendar) => { 1341 if (err) { 1342 console.error(`Failed to get calendar. Code: ${err.code}, message: ${err.message}`); 1343 } else { 1344 console.info(`Succeeded in getting calendar, data -> ${JSON.stringify(data)}`); 1345 calendar = data; 1346 await calendar.addEvent(event).then((data: number) => { 1347 console.info(`Succeeded in adding event, id -> ${data}`); 1348 }).catch((err: BusinessError) => { 1349 console.error(`Failed to add event. Code: ${err.code}, message: ${err.message}`); 1350 }); 1351 // Perform fuzzy query based on MyEvent. If an event of the MyEvent1 type exists, the event can also be queried. 1352 const filter = calendarManager.EventFilter.filterByTitle('MyEvent'); 1353 calendar.getEvents(filter).then((data: calendarManager.Event[]) => { 1354 console.info(`Succeeded in getting events, data -> ${JSON.stringify(data)}`); 1355 }).catch((err: BusinessError) => { 1356 console.error(`Failed to get events. Code: ${err.code}, message: ${err.message}`); 1357 }); 1358 } 1359}); 1360``` 1361 1362### getConfig 1363 1364getConfig(): CalendarConfig 1365 1366Obtains the calendar configuration information. 1367 1368**System capability**: SystemCapability.Applications.CalendarData 1369 1370**Return value** 1371 1372| Type | Description | 1373| --------------------------------- | -------------- | 1374| [CalendarConfig](#calendarconfig) | Calendar configuration information.| 1375 1376**Example** 1377 1378```typescript 1379import { calendarMgr } from '../entryability/EntryAbility'; 1380import { BusinessError } from '@kit.BasicServicesKit'; 1381 1382let calendar : calendarManager.Calendar | undefined = undefined; 1383calendarMgr?.getCalendar((err: BusinessError, data:calendarManager.Calendar) => { 1384 if (err) { 1385 console.error(`Failed to get calendar. Code: ${err.code}, message: ${err.message}`); 1386 } else { 1387 console.info(`Succeeded in getting calendar, data -> ${JSON.stringify(data)}`); 1388 calendar = data; 1389 const config = calendar.getConfig(); 1390 console.info(`Succeeded in getting config, config -> ${JSON.stringify(config)}`); 1391 } 1392}); 1393``` 1394 1395### setConfig 1396 1397setConfig(config: CalendarConfig, callback: AsyncCallback\<void>): void 1398 1399Sets the calendar configuration information. This API uses an asynchronous callback to return the result. 1400 1401**System capability**: SystemCapability.Applications.CalendarData 1402 1403**Parameters** 1404 1405| Name | Type | Mandatory| Description | 1406| -------- | --------------------------------- | ---- | -------------- | 1407| config | [CalendarConfig](#calendarconfig) | Yes | Calendar configuration information.| 1408| callback | AsyncCallback\<void> | Yes | Callback used to return the result. | 1409 1410**Example** 1411 1412```typescript 1413import { BusinessError } from '@kit.BasicServicesKit'; 1414import { calendarMgr } from '../entryability/EntryAbility'; 1415 1416let calendar : calendarManager.Calendar | undefined = undefined; 1417const config: calendarManager.CalendarConfig = { 1418 enableReminder: true, 1419 color: '#aabbcc' 1420}; 1421calendarMgr?.getCalendar((err: BusinessError, data:calendarManager.Calendar) => { 1422 if (err) { 1423 console.error(`Failed to get calendar. Code: ${err.code}, message: ${err.message}`); 1424 } else { 1425 console.info(`Succeeded in getting calendar, data -> ${JSON.stringify(data)}`); 1426 calendar = data; 1427 calendar.setConfig(config, (err: BusinessError) => { 1428 if (err) { 1429 console.error(`Failed to set config. Code: ${err.code}, message: ${err.message}`); 1430 } else { 1431 console.info(`Succeeded in setting config, config -> ${JSON.stringify(config)}`); 1432 } 1433 }); 1434 } 1435}); 1436``` 1437 1438### setConfig 1439 1440setConfig(config: CalendarConfig): Promise\<void> 1441 1442Sets the calendar configuration information. This API uses a promise to return the result. 1443 1444**System capability**: SystemCapability.Applications.CalendarData 1445 1446**Parameters** 1447 1448| Name| Type | Mandatory| Description | 1449| ------ | --------------------------------- | ---- | -------------- | 1450| config | [CalendarConfig](#calendarconfig) | Yes | Calendar configuration information.| 1451 1452**Return value** 1453 1454| Type | Description | 1455| -------------- | ------------------------- | 1456| Promise\<void> | Promise that returns no value.| 1457 1458**Example** 1459 1460```typescript 1461import { BusinessError } from '@kit.BasicServicesKit'; 1462import { calendarMgr } from '../entryability/EntryAbility'; 1463 1464let calendar : calendarManager.Calendar | undefined = undefined; 1465const config: calendarManager.CalendarConfig = { 1466 enableReminder: true, 1467 color: '#aabbcc' 1468}; 1469calendarMgr?.getCalendar((err: BusinessError, data:calendarManager.Calendar) => { 1470 if (err) { 1471 console.error(`Failed to get calendar. Code: ${err.code}, message: ${err.message}`); 1472 } else { 1473 console.info(`Succeeded in getting calendar, data -> ${JSON.stringify(data)}`); 1474 calendar = data; 1475 calendar.setConfig(config).then(() => { 1476 console.info(`Succeeded in setting config, data->${JSON.stringify(config)}`); 1477 }).catch((err: BusinessError) => { 1478 console.error(`Failed to set config. Code: ${err.code}, message: ${err.message}`); 1479 }); 1480 } 1481}); 1482``` 1483 1484### getAccount 1485 1486getAccount(): CalendarAccount 1487 1488Obtains the calendar account information. 1489 1490**System capability**: SystemCapability.Applications.CalendarData 1491 1492**Return value** 1493 1494| Type | Description | 1495| ----------------------------------- | -------------- | 1496| [CalendarAccount](#calendaraccount) | Calendar account information.| 1497 1498**Example** 1499 1500```typescript 1501import { calendarMgr } from '../entryability/EntryAbility'; 1502import { BusinessError } from '@kit.BasicServicesKit'; 1503 1504let calendar : calendarManager.Calendar | undefined = undefined; 1505calendarMgr?.getCalendar((err: BusinessError, data:calendarManager.Calendar) => { 1506 if (err) { 1507 console.error(`Failed to get calendar. Code: ${err.code}, message: ${err.message}`); 1508 } else { 1509 console.info(`Succeeded in getting calendar, data -> ${JSON.stringify(data)}`); 1510 calendar = data; 1511 const account = calendar.getAccount(); 1512 console.info(`succeeded in getting account, account -> ${JSON.stringify(account)}`); 1513 } 1514}); 1515``` 1516 1517### queryEventInstances<sup>18+</sup> 1518 1519queryEventInstances(start: number, end: number, ids?: number[], eventKey?: (keyof Event)[]): Promise\<Event[]> 1520 1521Queries the event instance with a specified event key in a calendar. This API uses a promise to return the result. 1522 1523**System capability**: SystemCapability.Applications.CalendarData 1524 1525**Atomic service API**: This API can be used in atomic services since API version 18. 1526 1527**Parameters** 1528 1529| Name | Type | Mandatory | Description | 1530| ----------- | --------------------------- |------|------------| 1531| start | number | Yes | Start time of an event. The value is a 13-digit timestamp. | 1532| end | number | Yes | End time of an event. The value is a 13-digit timestamp. | 1533| ids | number[] | No | Array of event IDs to be queried, which can be empty or undefined. Otherwise, the value of each ID must be greater than **0**. | 1534| eventKey | (keyof [Event](#event))[] | No | Event key for querying events.| 1535 1536**Return value** 1537 1538| Type | Description | 1539| -------------------------- | ----------------------------------- | 1540| Promise<[Event](#event)[]> | Promise used to return the result, which is an array of **Event** objects.| 1541 1542**Example** 1543 1544```typescript 1545import { BusinessError } from '@kit.BasicServicesKit'; 1546import { calendarMgr } from '../entryability/EntryAbility'; 1547 1548let calendar : calendarManager.Calendar | undefined = undefined; 1549const date = new Date(); 1550const event: calendarManager.Event = { 1551 title: 'MyEvent', 1552 type: calendarManager.EventType.IMPORTANT, 1553 startTime: date.getTime(), 1554 endTime: date.getTime() + 60 * 60 * 1000 1555}; 1556calendarMgr?.getCalendar(async (err: BusinessError, data:calendarManager.Calendar) => { 1557 if (err) { 1558 console.error(`Failed to get calendar. Code: ${err.code}, message: ${err.message}`); 1559 } else { 1560 console.info(`Succeeded in getting calendar, data -> ${JSON.stringify(data)}`); 1561 calendar = data; 1562 await calendar.addEvent(event).then((data: number) => { 1563 console.info(`Succeeded in adding event, id -> ${data}`); 1564 }).catch((err: BusinessError) => { 1565 console.error(`Failed to add event. Code: ${err.code}, message: ${err.message}`); 1566 }); 1567 calendar?.queryEventInstances(date.getTime(), date.getTime() + 60 * 60 * 1000, undefined, 1568 ["title", "startTime", "endTime", "instanceStartTime", "instanceEndTime",]).then((data: calendarManager.Event[]) => { 1569 console.info(`Succeeded in getting event instances, data -> ${JSON.stringify(data)}`); 1570 }).catch((err: BusinessError) => { 1571 console.error(`Failed to get event instances. Code: ${err.code}, message: ${err.message}`); 1572 }); 1573 } 1574}); 1575``` 1576 1577## CalendarAccount 1578 1579Describes the calendar account information. 1580 1581**Atomic service API**: This API can be used in atomic services since API version 11. 1582 1583**System capability**: SystemCapability.Applications.CalendarData 1584 1585| Name | Type | Read Only| Optional| Description | 1586| ----------- | ----------------------------- | ---- |----|----------------------------------------------| 1587| name | string | Yes | No | Account name (defined by developers), with a maximum of 5000 characters. | 1588| type | [CalendarType](#calendartype) | No | No | Account type. | 1589| displayName | string | No | Yes | Account name displayed on the calendar application (defined by users). If this parameter is not specified, the default value is an empty string with a maximum of 64 characters.| 1590 1591## CalendarConfig 1592 1593Describes the calendar configuration information. 1594 1595**System capability**: SystemCapability.Applications.CalendarData 1596 1597| Name | Type | Read Only | Optional| Description | 1598| -------------- |--------|-------|----| ------------------------------------------------------------ | 1599| enableReminder | boolean | No | Yes | Whether to enable the reminder for events in the calendar. The value **true** means to enable the reminder for events in the calendar, and **false** means the opposite. The default value is **true**.| 1600| color | number \| string | No | Yes | Calendar color. If the value is a number, the value ranges from 0x000000 to 0xFFFFFF or from 0x00000000 to 0xFFFFFFFF. If the value is a string, the value contains 7 or 9 characters, for example, **#FFFFFF** or **#FFFFFFFFF**. If this parameter is not specified or the input data is incorrect, the default value **#0A59F7** is used.| 1601 1602## Event 1603 1604Describes an **Event** object, including the event title, start time, and end time. 1605 1606**System capability**: SystemCapability.Applications.CalendarData 1607 1608| Name | Type | Read Only| Optional| Description | 1609| -------------- | --------------------------------- | ---- |----|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| 1610| id | number | No | Yes | Event ID. When you call [addEvent()](#addevent) or [addEvents()](#addevents) to create an event, this parameter is not required. When you call [deleteEvent()](#deleteevent) or [deleteEvents()](#deleteevents) to delete an event, this parameter is required and must be set to an array of positive integers. If this parameter is set to an invalid value, an error will be reported.<br>**Atomic service API**: This API can be used in atomic services since API version 11. | 1611| type | [EventType](#eventtype) | No | No | Event type.<br>**Atomic service API**: This API can be used in atomic services since API version 11. | 1612| title | string | No | Yes | Event title, with a maximum of 5000 characters. If this parameter is not specified, the default value is an empty string.<br>**Atomic service API**: This API can be used in atomic services since API version 11. | 1613| location | [Location](#location) | No | Yes | Event location. If this parameter is not set, the default null value is used.<br>**Atomic service API**: This API can be used in atomic services since API version 11. | 1614| startTime | number | No | No | Start time of the event. The value is a 13-digit timestamp.<br>**Atomic service API**: This API can be used in atomic services since API version 11. | 1615| endTime | number | No | No | End time of the event. The value is a 13-digit timestamp.<br>**Atomic service API**: This API can be used in atomic services since API version 11. | 1616| isAllDay | boolean | No | Yes | Whether the event is an all-day event. The value **true** means that the event is an all-day event, and **false** means the opposite. The default value is **false**.<br>**Atomic service API**: This API can be used in atomic services since API version 11. | 1617| attendee | [Attendee](#attendee)[] | No | Yes | Attendees of a conference event. If this parameter is not set, the default null value is used.<br>**Atomic service API**: This API can be used in atomic services since API version 11. | 1618| timeZone | string | No | Yes | Time zone of the event, with a maximum of 5000 characters. If this parameter is not specified or set to an invalid value, the current time zone is used by default. If a different time zone is required, enter the corresponding time zone. You can call [systemDateTime.getTimezone()](../apis-basic-services-kit/js-apis-date-time.md#systemdatetimegettimezone) to obtain the current system time zone.<br>**Atomic service API**: This API can be used in atomic services since API version 11.| 1619| reminderTime | number[] | No | Yes | Amount of time that the reminder occurs before the start of the event, in minutes. For example, if the value is 5, the reminder occurs 5 minutes before the event starts. If this parameter is not set, no reminder is set. A negative value indicates the delay time for sending a notification.<br>**Atomic service API**: This API can be used in atomic services since API version 11. | 1620| recurrenceRule | [RecurrenceRule](#recurrencerule) | No | Yes | Recurrence rule of an event. If this parameter is not set, the value does not recur.<br>**Atomic service API**: This API can be used in atomic services since API version 11. | 1621| description | string | No | Yes | Event description, with a maximum of 5000 characters. If this parameter is not specified, the default value is an empty string.<br>**Atomic service API**: This API can be used in atomic services since API version 11. | 1622| service | [EventService](#eventservice) | No | Yes | <!--RP1-->Event service. If this parameter is not set, no service is available. This function is not supported currently.<!--RP1End--> <br>**Atomic service API**: This API can be used in atomic services since API version 11. | 1623| identifier<sup>12+</sup> | string | No | Yes | Unique ID of an event, with a maximum of 5000 characters. If this parameter is not specified, the default value is **null**.<br>**Atomic service API**: This API can be used in atomic services since API version 12. | 1624| isLunar<sup>12+</sup> | boolean | No | Yes | Whether it is a lunar calendar event. The value **true** means that the event is a lunar calendar event, and **false** means the opposite. The default value is **false**.<br>**Atomic service API**: This API can be used in atomic services since API version 12. | 1625| instanceStartTime<sup>18+</sup> | number | No | Yes | Start time of the event. The value is a 13-digit timestamp. This parameter does not need to be set in [addEvent()](#addevent) or [addEvents()](#addevents).<br>**Atomic service API**: This API can be used in atomic services since API version 18. | 1626| instanceEndTime<sup>18+</sup> | number | No | Yes | End time of the event. The value is a 13-digit timestamp. This parameter does not need to be set in [addEvent()](#addevent) or [addEvents()](#addevents).<br>**Atomic service API**: This API can be used in atomic services since API version 18. | 1627 1628## CalendarType 1629 1630Enumerates the account types. 1631 1632**Atomic service API**: This API can be used in atomic services since API version 11. 1633 1634**System capability**: SystemCapability.Applications.CalendarData 1635 1636| Name | Value | Description | 1637| ---------- | ------------ | -------------------- | 1638| LOCAL | 'local' | Local account. | 1639| EMAIL | 'email' | Email account. | 1640| BIRTHDAY | 'birthday' | Birthday account. | 1641| CALDAV | 'caldav' | CalDAV account.| 1642| SUBSCRIBED | 'subscribed' | Subscription account. | 1643 1644## Location 1645 1646Describes the event location. 1647 1648**Atomic service API**: This API can be used in atomic services since API version 11. 1649 1650**System capability**: SystemCapability.Applications.CalendarData 1651 1652| Name | Type | Read Only| Optional| Description | 1653| --------- | ------ | ---- |----| ------------------------ | 1654| location | string | No | Yes | Location, with a maximum of 5000 characters. If this parameter is not specified, the default value is an empty string.| 1655| longitude | number | No | Yes | Longitude of the location. The value ranges from -180 to 180. The default value is **0**. | 1656| latitude | number | No | Yes | Latitude of the location. The value ranges from -90 to 90. The default value is **0**. | 1657 1658## EventFilter 1659 1660Implements an event filter. 1661 1662You can use [filterById()](#filterbyid), [filterByTime()](#filterbytime), or [filterByTitle()](#filterbytitle) to obtain an event filter, and then pass the filter in [getEvents()](#getevents) for filtering. 1663 1664**System capability**: SystemCapability.Applications.CalendarData 1665 1666### filterById 1667 1668static filterById(ids: number[]): EventFilter 1669 1670Defines an event ID based filter. 1671 1672**System capability**: SystemCapability.Applications.CalendarData 1673 1674**Parameters** 1675 1676| Name| Type | Mandatory| Description | 1677| ------ | -------- | ---- | ------------ | 1678| ids | number[] | Yes | An array of event IDs, where each event ID must be a positive integer.| 1679 1680**Return value** 1681 1682| Type | Description | 1683| --------------------------- | -------------------- | 1684| [EventFilter](#eventfilter) | **EventFilter** object.| 1685 1686**Example** 1687 1688```typescript 1689import { BusinessError } from '@kit.BasicServicesKit'; 1690import { calendarMgr } from '../entryability/EntryAbility'; 1691 1692let calendar : calendarManager.Calendar | undefined = undefined; 1693let id1: number = 0; 1694let id2: number = 0; 1695const date = new Date(); 1696const event1: calendarManager.Event = { 1697 type: calendarManager.EventType.NORMAL, 1698 startTime: date.getTime(), 1699 endTime: date.getTime() + 60 * 60 * 1000 1700}; 1701const event2: calendarManager.Event = { 1702 type: calendarManager.EventType.IMPORTANT, 1703 startTime: date.getTime(), 1704 endTime: date.getTime() + 60 * 60 * 1000 1705}; 1706calendarMgr?.getCalendar(async (err: BusinessError, data:calendarManager.Calendar) => { 1707 if (err) { 1708 console.error(`Failed to get calendar. Code: ${err.code}, message: ${err.message}`); 1709 } else { 1710 console.info(`Succeeded in getting calendar, data -> ${JSON.stringify(data)}`); 1711 calendar = data; 1712 await calendar.addEvent(event1).then((data: number) => { 1713 console.info(`Succeeded in adding event, id -> ${data}`); 1714 id1 = data; 1715 }).catch((err: BusinessError) => { 1716 console.error(`Failed to add event. Code: ${err.code}, message: ${err.message}`); 1717 }); 1718 await calendar.addEvent(event2).then((data: number) => { 1719 console.info(`Succeeded in adding event, id -> ${data}`); 1720 id2 = data; 1721 }).catch((err: BusinessError) => { 1722 console.error(`Failed to add event. Code: ${err.code}, message: ${err.message}`); 1723 }); 1724 const filter = calendarManager.EventFilter.filterById([id1, id2]); 1725 calendar.getEvents(filter).then((data: calendarManager.Event[]) => { 1726 console.info(`Succeeded in getting events filter by id, data -> ${JSON.stringify(data)}`); 1727 }).catch((err: BusinessError) => { 1728 console.error(`Failed to filter by id. Code: ${err.code}, message: ${err.message}`); 1729 }); 1730 } 1731}); 1732``` 1733 1734### filterByTime 1735 1736static filterByTime(start: number, end: number): EventFilter 1737 1738Defines an event time based filter. 1739 1740**System capability**: SystemCapability.Applications.CalendarData 1741 1742**Parameters** 1743 1744| Name| Type | Mandatory| Description | 1745| ------ | ------ | ---- | ---------- | 1746| start | number | Yes | Start time. The value is a 13-digit timestamp.| 1747| end | number | Yes | End time. The value is a 13-digit timestamp.| 1748 1749**Return value** 1750 1751| Type | Description | 1752| --------------------------- | -------------------- | 1753| [EventFilter](#eventfilter) | **EventFilter** object.| 1754 1755**Example** 1756 1757```typescript 1758import { BusinessError } from '@kit.BasicServicesKit'; 1759import { calendarMgr } from '../entryability/EntryAbility'; 1760 1761let calendar : calendarManager.Calendar | undefined = undefined; 1762const event1: calendarManager.Event = { 1763 type: calendarManager.EventType.NORMAL, 1764 startTime: 1686931200000, 1765 endTime: 1687017600000 1766}; 1767const event2: calendarManager.Event = { 1768 type: calendarManager.EventType.IMPORTANT, 1769 startTime: 1686931200000, 1770 endTime: 1687017600000 1771}; 1772calendarMgr?.getCalendar(async (err: BusinessError, data:calendarManager.Calendar) => { 1773 if (err) { 1774 console.error(`Failed to get calendar. Code: ${err.code}, message: ${err.message}`); 1775 } else { 1776 console.info(`Succeeded in getting calendar, data -> ${JSON.stringify(data)}`); 1777 calendar = data; 1778 await calendar.addEvent(event1).then((data: number) => { 1779 console.info(`Succeeded in adding event, id -> ${data}`); 1780 }).catch((err: BusinessError) => { 1781 console.error(`Failed to add event. Code: ${err.code}, message: ${err.message}`); 1782 }); 1783 await calendar.addEvent(event2).then((data: number) => { 1784 console.info(`Succeeded in adding event, id -> ${data}`); 1785 }).catch((err: BusinessError) => { 1786 console.error(`Failed to add event. Code: ${err.code}, message: ${err.message}`); 1787 }); 1788 const filter = calendarManager.EventFilter.filterByTime(1686931200000, 1687017600000); 1789 calendar.getEvents(filter).then((data: calendarManager.Event[]) => { 1790 console.info(`Succeeded in getting events filter by time, data -> ${JSON.stringify(data)}`); 1791 }).catch((err: BusinessError) => { 1792 console.error(`Failed to filter by time. Code: ${err.code}, message: ${err.message}`); 1793 }); 1794 } 1795}); 1796``` 1797 1798### filterByTitle 1799 1800static filterByTitle(title: string): EventFilter 1801 1802Filters events by event title. This API supports fuzzy match. 1803 1804**System capability**: SystemCapability.Applications.CalendarData 1805 1806**Parameters** 1807 1808| Name| Type | Mandatory| Description | 1809| ------ | ------ | ---- | ---------- | 1810| title | string | Yes | Event title, with a maximum of 5000 characters.| 1811 1812**Return value** 1813 1814| Type | Description | 1815| --------------------------- | -------------------- | 1816| [EventFilter](#eventfilter) | **EventFilter** object.| 1817 1818**Example** 1819 1820```typescript 1821import { BusinessError } from '@kit.BasicServicesKit'; 1822import { calendarMgr } from '../entryability/EntryAbility'; 1823 1824let calendar : calendarManager.Calendar | undefined = undefined; 1825const event: calendarManager.Event = { 1826 title: 'MyEvent', 1827 type: calendarManager.EventType.NORMAL, 1828 startTime: 1686931200000, 1829 endTime: 1687017600000 1830}; 1831calendarMgr?.getCalendar(async (err: BusinessError, data:calendarManager.Calendar) => { 1832 if (err) { 1833 console.error(`Failed to get calendar. Code: ${err.code}, message: ${err.message}`); 1834 } else { 1835 console.info(`Succeeded in getting calendar, data -> ${JSON.stringify(data)}`); 1836 calendar = data; 1837 await calendar.addEvent(event).then((data: number) => { 1838 console.info(`Succeeded in adding event, id -> ${data}`); 1839 }).catch((err: BusinessError) => { 1840 console.error(`Failed to add event. Code: ${err.code}, message: ${err.message}`); 1841 }); 1842 const filter = calendarManager.EventFilter.filterByTitle('MyEvent'); 1843 calendar.getEvents(filter).then((data: calendarManager.Event[]) => { 1844 console.info(`Succeeded in getting events filter by title, data -> ${JSON.stringify(data)}`); 1845 }).catch((err: BusinessError) => { 1846 console.error(`Failed to filter by title. Code: ${err.code}, message: ${err.message}`); 1847 }); 1848 } 1849}); 1850``` 1851 1852## EventType 1853 1854Enumerates event types. 1855 1856**Atomic service API**: This API can be used in atomic services since API version 11. 1857 1858**System capability**: SystemCapability.Applications.CalendarData 1859 1860| Name | Value | Description | 1861| --------- | ---- |-------------------------| 1862| NORMAL | 0 | Normal event, such as conference or an alarm clock. | 1863| IMPORTANT | 1 | Important event, such as wedding anniversary, are not recommended for third-party developers. Important events do not support one-click service redirection and custom reminder time.| 1864 1865## RecurrenceRule 1866 1867Describes the recurrence rule of an event. 1868 1869**System capability**: SystemCapability.Applications.CalendarData 1870 1871| Name | Type | Read Only| Optional| Description | 1872| ------------------- | ------------------------------------------- | ---- |----|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| 1873| recurrenceFrequency | [RecurrenceFrequency](#recurrencefrequency) | No | No | Type of the event recurrence rule.<br>**Atomic service API**: This API can be used in atomic services since API version 11. | 1874| expire | number | No | Yes | End date of the recurrence period. The value is a 13-digit timestamp. If this parameter is not specified, the event has no end date.<br>**Atomic service API**: This API can be used in atomic services since API version 11. | 1875| count<sup>12+</sup> | number | No | Yes | Number of the recurrent event. The value is a non-negative integer. If this parameter is not set, the default value **0** is used, indicating that the number of recurrent events is infinite; if the value is negative, the effect is the same as that of **0**. If both **count** and **expire** exist, **count** is used.<br>**Atomic service API**: This API can be used in atomic services since API version 12. | 1876| interval<sup>12+</sup> | number | No | Yes | Interval for a recurrent event. The value is a non-negative integer. If this parameter is not set, the default value **0** is used, indicating that the event is repeated based on the recurrence rule without intervals; if the value is negative, the effect is the same as that of **0**. If both **interval** and **expire** exist, **expire** is used.<br>This attribute is related to the **recurrenceFrequency** rule. The recurrence interval varies according to the recurrence rule. For example, if the **interval** value is **2**, the following situations occur:<br>Daily recurrence: The event repeats every two days.<br>Weekly recurrence: The event repeats every two weeks.<br>Monthly recurrence: The event repeats every two months.<br>Yearly recurrence: The event repeats every two years.<br>**Atomic service API**: This API can be used in atomic services since API version 12.| 1877| excludedDates<sup>12+</sup> | number[] | No | Yes | Excluded date of a recurrent event. The value is in the timestamp format. If this parameter is not set, the default value is empty, indicating that no date is excluded; if the value is **0** or a negative number, the effect is the same as that of the empty value.<br>**Atomic service API**: This API can be used in atomic services since API version 12. | 1878| daysOfWeek<sup>12+</sup> | number[] | No | Yes | Repeats by day of a week. If this parameter is not set, the default value is empty, indicating that there is no recurrence rule. The value ranges from 1 to 7, corresponding to Monday to Sunday. Other values are invalid and have the same effect as the empty value.<br>**Atomic service API**: This API can be used in atomic services since API version 12. | 1879| daysOfMonth<sup>12+</sup> | number[] | No | Yes | Repeats by day of a month. If this parameter is not set, the default value is empty, indicating that there is no recurrence rule. The value ranges from 1 to 31, corresponding to the first to the last days of each month. Other values are invalid and have the same effect as **null**. The value **29**, **30**, or **31** is invalid if the corresponding date does not exist in the current month.<br>**Atomic service API**: This API can be used in atomic services since API version 12. | 1880| daysOfYear<sup>12+</sup> | number[] | No | Yes | Repeats by day of a year. If this parameter is not set, the default value is empty, indicating that there is no recurrence rule. The value ranges from 1 to 366, corresponding to the first to the last days of each year. Other values are invalid and have the same effect as the empty value. If this year only has 365 days, the value **366** is invalid.<br>**Atomic service API**: This API can be used in atomic services since API version 12. | 1881| weeksOfMonth<sup>12+</sup> | number[] | No | Yes | Repeats by week of a month. If this parameter is not set, the default value is empty, indicating that there is no recurrence rule. The value ranges from 1 to 5, corresponding to the first to the last weeks of each month. Other values are invalid and have the same effect as the empty value. If this month only has four weeks, the value **5** is invalid.<br>**Atomic service API**: This API can be used in atomic services since API version 12. | 1882| weeksOfYear<sup>12+</sup> | number[] | No | Yes | Repeats by week of a year. If this parameter is not set, the default value is empty, indicating that there is no recurrence rule. The value ranges from 1 to 53, corresponding to the first to the last weeks of each year. Other values are invalid and have the same effect as the empty value.<br>**Atomic service API**: This API can be used in atomic services since API version 12. | 1883| monthsOfYear<sup>12+</sup> | number[] | No | Yes | Repeats by month of a year. If this parameter is not set, the default value is empty, indicating that there is no recurrence rule. The value ranges from 1 to 12, corresponding to the first to the last months of each year. Other values are invalid and have the same effect as the empty value.<br>**Atomic service API**: This API can be used in atomic services since API version 12. | 1884## RecurrenceFrequency 1885 1886Enumerates the types of the event recurrence rule. 1887 1888**Atomic service API**: This API can be used in atomic services since API version 11. 1889 1890**System capability**: SystemCapability.Applications.CalendarData 1891 1892| Name | Value | Description | 1893| ------- | ---- | ---------- | 1894| YEARLY | 0 | Yearly.| 1895| MONTHLY | 1 | Monthly.| 1896| WEEKLY | 2 | Weekly.| 1897| DAILY | 3 | Daily.| 1898 1899## Attendee 1900 1901Describes the attendee information in a conference event. 1902 1903**System capability**: SystemCapability.Applications.CalendarData 1904 1905| Name | Type | Read Only| Optional| Description | 1906| ----- | ------ | ---- |----|--------------------------------------------------------------------| 1907| name | string | No | No | Name of the attendee, with a maximum of 5000 characters.<br>**Atomic service API**: This API can be used in atomic services since API version 11. | 1908| email | string | No | No | Email address of the attendee, with a maximum of 5000 characters.<br>**Atomic service API**: This API can be used in atomic services since API version 11.| 1909| role<sup>12+</sup> | [AttendeeRole](#attendeerole12) | No | Yes | Role of the attendee.<br>**Atomic service API**: This API can be used in atomic services since API version 12. | 1910| status<sup>18+</sup> | [AttendeeStatus](#attendeestatus18) | No | Yes| Status of the attendee. If this parameter is not set, the default value is empty.<br>**Atomic service API**: This API can be used in atomic services since API version 18.| 1911| type<sup>18+</sup> | [AttendeeType](#attendeetype18) | No | Yes| Type of the attendee. If this parameter is not set, the default value is empty.<br>**Atomic service API**: This API can be used in atomic services since API version 18.| 1912 1913## EventService 1914 1915Describes the event service. 1916 1917**Atomic service API**: This API can be used in atomic services since API version 11. 1918 1919**System capability**: SystemCapability.Applications.CalendarData 1920 1921| Name | Type | Read Only| Optional| Description | 1922| ----------- | --------------------------- | ---- |----|-------------------------------------| 1923| type | [ServiceType](#servicetype) | No | No | Service type. | 1924| uri | string | No | No | Service URI, in the DeepLink format. It can be used to redirect the user to a page of another application. The value is a string with a maximum of 5000 characters.| 1925| description | string | No | Yes | Description of the service, with a maximum of 5000 characters. If this parameter is not specified, the default value is an empty string. | 1926 1927## ServiceType 1928 1929Enumerates the event service types. 1930 1931**Atomic service API**: This API can be used in atomic services since API version 11. 1932 1933**System capability**: SystemCapability.Applications.CalendarData 1934 1935| Name | Value | Description | 1936| --------------- | ---------------- | ------------ | 1937| MEETING | 'Meeting' | Join a meeting. | 1938| WATCHING | 'Watching' | Watch a video. | 1939| REPAYMENT | 'Repayment' | Make a payment. | 1940| LIVE | 'Live' | Watch live TV. | 1941| SHOPPING | 'Shopping' | Go shopping. | 1942| TRIP | 'Trip' | View the trip. | 1943| CLASS | 'Class' | Join class. | 1944| SPORTS_EVENTS | 'SportsEvents' | Watch a sports event.| 1945| SPORTS_EXERCISE | 'SportsExercise' | Start exercising. | 1946 1947## AttendeeRole<sup>12+</sup> 1948 1949Enumerates the attendee role types in a conference event. 1950 1951**Atomic service API**: This API can be used in atomic services since API version 12. 1952 1953**System capability**: SystemCapability.Applications.CalendarData 1954 1955| Name | Value | Description | 1956|--------------|---------------|--------| 1957| ORGANIZER | 'organizer' | Conference organizer.| 1958| PARTICIPANT | 'participant' | Conference participant.| 1959 1960## AttendeeStatus<sup>18+</sup> 1961 1962Enumerates the status types of an attendee. 1963 1964**Atomic service API**: This API can be used in atomic services since API version 18. 1965 1966**System capability**: SystemCapability.Applications.CalendarData 1967 1968| Name | Value | Description | 1969|----------------------------|-----|----------| 1970| UNKNOWN | 0 | The attendee status is unknown.| 1971| TENTATIVE | 1 | The attendee status is tentative.| 1972| ACCEPTED | 2 | The attendee has accepted the conference invitation. | 1973| DECLINED | 3 | The attendee has rejected the conference invitation. | 1974| UNRESPONSIVE | 4 | The attendee does not respond. | 1975 1976## AttendeeType<sup>18+</sup> 1977 1978Enumerates the types of attendees invited to a conference event. 1979 1980**Atomic service API**: This API can be used in atomic services since API version 18. 1981 1982**System capability**: SystemCapability.Applications.CalendarData 1983 1984| Name | Value | Description | 1985|------------------------|-----|--------------------| 1986| REQUIRED | 1 | Required attendee. | 1987| OPTIONAL | 2 | Optional attendee. | 1988| RESOURCE | 3 | Resources (such as TVs or projectors) used in a conference.| 1989