1# @ohos.calendarManager (Calendar Manager) 2 3The **calendarManager** module provides APIs for calendar and event management, including those for creating, deleting, modifying, and querying calendars and events. A [CalendarManager](#calendarmanager) object is used to manage [Calendar](#calendar) objects. A [Calendar](#calendar) object contains the account information [CalendarAccount](#calendaraccount) and configuration information [CalendarConfig](#calendarconfig). Calendars and events are in the one-to-many relationship. That is, a calendar can have multiple events, but an event belongs to only one calendar. 4 5> **NOTE** 6> 7> 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. 8 9 10## Modules to Import 11 12```typescript 13import calendarManager from '@ohos.calendarManager'; 14``` 15 16## calendarManager.getCalendarManager 17 18getCalendarManager(context : Context): CalendarManager 19 20Obtains a **CalendarManager** object based on the context. 21 22**System capability**: SystemCapability.Applications.CalendarData 23 24**Model restriction**: This API can be used only in the stage model. 25 26**Parameters** 27 28| Name | Type | Mandatory| Description | 29| -------- | --------------------------- | ---- | ------------------------------------------------------------ | 30| context | Context | Yes | Application context. For details about the application context of the stage model, see [Context]().| 31 32**Return value** 33 34| Type | Description | 35| ------------------------------ | ------------------------------------- | 36| CalendarManager | **CalendarManager** object obtained.| 37 38**Example** 39 40```typescript 41// Obtain an mContext object. 42// Obtain a calendarMgr object. 43// The file is auto-generated: entry/src/main/ets/entryability/EntryAbility.ets 44import AbilityConstant from '@ohos.app.ability.AbilityConstant'; 45import hilog from '@ohos.hilog'; 46import UIAbility from '@ohos.app.ability.UIAbility'; 47import Want from '@ohos.app.ability.Want'; 48import window from '@ohos.window'; 49import common from '@ohos.app.ability.common'; 50import abilityAccessCtrl, { PermissionRequestResult, Permissions } from '@ohos.abilityAccessCtrl'; 51import { BusinessError } from '@ohos.base'; 52 53export let calendarMgr: calendarManager.CalendarManager | null = null; 54export let mContext: common.UIAbilityContext | null = null; 55export default class EntryAbility extends UIAbility { 56 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { 57 hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); 58 } 59 60 onDestroy(): void { 61 hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); 62 } 63 64 onWindowStageCreate(windowStage: window.WindowStage): void { 65 // Main window is created, set main page for this ability 66 hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); 67 68 windowStage.loadContent('pages/Index', (err, data) => { 69 if (err.code) { 70 hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); 71 return; 72 } 73 hilog.info(0x0000, 'testTag', 'Succeeded in loading the content. Data: %{public}s', JSON.stringify(data) ?? ''); 74 }); 75 mContext = this.context; 76 const permissions: Permissions[] = ['ohos.permission.READ_CALENDAR', 'ohos.permission.WRITE_CALENDAR']; 77 let atManager = abilityAccessCtrl.createAtManager(); 78 atManager.requestPermissionsFromUser(mContext, permissions).then((result: PermissionRequestResult) => { 79 console.log(`get Permission success, result: ${JSON.stringify(result)}`); 80 calendarMgr = calendarManager.getCalendarManager(mContext); 81 }).catch((error: BusinessError) => { 82 console.error(`get Permission error, error: ${JSON.stringify(error)}`); 83 }) 84 } 85 86 onWindowStageDestroy(): void { 87 // Main window is destroyed, release UI related resources 88 hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); 89 } 90 91 onForeground(): void { 92 // Ability has brought to foreground 93 hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); 94 } 95 96 onBackground(): void { 97 // Ability has back to background 98 hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); 99 } 100} 101``` 102 103## CalendarManager 104 105Before calling any of the following APIs, you must use [getCalendarManager()](#calendarmanagergetcalendarmanager) to obtain a **CalendarManager** object. 106 107 108### createCalendar 109 110createCalendar(calendarAccount: CalendarAccount, callback: AsyncCallback\<Calendar>): void 111 112Creates a **Calendar** object based on the calendar account information. This API uses an asynchronous callback to return the result. 113 114**Required permissions**: ohos.permission.WRITE_CALENDAR or ohos.permission.WRITE_WHOLE_CALENDAR 115 116**System capability**: SystemCapability.Applications.CalendarData 117 118**Parameters** 119 120| Name | Type | Mandatory| Description | 121| --------------- | ------------------------------------- | ---- | ---------------------------------- | 122| calendarAccount | [CalendarAccount](#calendaraccount) | Yes | Calendar account information. | 123| callback | AsyncCallback\<[Calendar](#calendar)> | Yes | Callback used to return the created **Calendar** object.| 124 125**Example** 126 127```typescript 128import { BusinessError } from '@ohos.base'; 129import { calendarMgr } from '../entryability/EntryAbility'; 130 131let calendar: calendarManager.Calendar | undefined = undefined; 132const calendarAccount: calendarManager.CalendarAccount = { 133 name: 'CreateMyCalendarByCallBack', 134 type: calendarManager.CalendarType.LOCAL 135}; 136try { 137 calendarMgr?.createCalendar(calendarAccount, (err: BusinessError, data: calendarManager.Calendar) => { 138 if (err) { 139 console.error(`Failed to create calendar, err -> ${JSON.stringify(err)}`); 140 } else { 141 console.info(`Succeeded in creating the calendar, data -> ${JSON.stringify(data)}`); 142 calendar = data; 143 } 144 }); 145} catch (error) { 146 console.error(`Failed to create calendar: err->${JSON.stringify(error)}`); 147} 148``` 149 150### createCalendar 151 152createCalendar(calendarAccount: CalendarAccount): Promise\<Calendar> 153 154Creates a **Calendar** object based on the calendar account information. This API uses a promise to return the result. 155 156**Required permissions**: ohos.permission.WRITE_CALENDAR or ohos.permission.WRITE_WHOLE_CALENDAR 157 158**System capability**: SystemCapability.Applications.CalendarData 159 160**Parameters** 161 162| Name | Type | Mandatory| Description | 163| --------------- | ----------------------------------- | ---- | -------------- | 164| calendarAccount | [CalendarAccount](#calendaraccount) | Yes | Calendar account information.| 165 166**Return value** 167 168| Type | Description | 169| ------------------------------ | ------------------------------------- | 170| Promise<[Calendar](#calendar)> | Promise used to return the created **Calendar** object.| 171 172**Example** 173 174```typescript 175import { BusinessError } from '@ohos.base'; 176import { calendarMgr } from '../entryability/EntryAbility'; 177 178let calendar : calendarManager.Calendar | undefined = undefined; 179const calendarAccount: calendarManager.CalendarAccount = { 180 name: 'CreateMyCalendarByPromise', 181 type: calendarManager.CalendarType.LOCAL, 182 displayName : 'MyApplication' 183}; 184calendarMgr?.createCalendar(calendarAccount).then((data: calendarManager.Calendar) => { 185 console.info(`Succeeded in creating the calendar data->${JSON.stringify(data)}`); 186 calendar = data; 187}).catch((error : BusinessError) => { 188 console.error(`Failed to create calendar: err->${JSON.stringify(error)}`); 189}); 190``` 191 192### deleteCalendar 193 194deleteCalendar(calendar: Calendar, callback: AsyncCallback\<void>): void 195 196Deletes a specified **Calendar** object. This API uses an asynchronous callback to return the result. 197 198**Required permissions**: ohos.permission.WRITE_CALENDAR or ohos.permission.WRITE_WHOLE_CALENDAR 199 200**System capability**: SystemCapability.Applications.CalendarData 201 202**Parameters** 203 204| Name | Type | Mandatory| Description | 205| -------- | --------------------- | ---- | -------------- | 206| calendar | [Calendar](#calendar) | Yes | **Calendar** object to delete.| 207| callback | AsyncCallback\<void> | Yes | Asynchronous callback that returns no value. | 208 209**Example** 210 211```typescript 212import { BusinessError } from '@ohos.base'; 213import { calendarMgr } from '../entryability/EntryAbility'; 214 215const calendarAccount: calendarManager.CalendarAccount = { 216 name: 'DeleteMyCalendarByCallBack', 217 type: calendarManager.CalendarType.LOCAL 218}; 219calendarMgr?.createCalendar(calendarAccount).then((data: calendarManager.Calendar) => { 220 console.info(`Succeeded in creating the calendar, data -> ${JSON.stringify(data)}`); 221 calendarMgr?.getCalendar(calendarAccount, (err: BusinessError, data: calendarManager.Calendar) => { 222 if (err) { 223 console.error(`Failed to get the calendar, err -> ${JSON.stringify(err)}`); 224 } else { 225 console.info(`Succeeded in getting the calendar, data -> ${JSON.stringify(data)}`); 226 calendarMgr?.deleteCalendar(data, (err1: BusinessError) => { 227 if (err1) { 228 console.error(`Failed to delete calendar, err -> ${JSON.stringify(err1)}`); 229 } else { 230 console.info("Succeeded in deleting the calendar"); 231 } 232 }); 233 } 234 }); 235}).catch((error: BusinessError) => { 236 console.error(`Failed to create calendar, error -> ${JSON.stringify(error)}`); 237}) 238``` 239 240### deleteCalendar 241 242deleteCalendar(calendar: Calendar): Promise\<void> 243 244Deletes a specified **Calendar** object. This API uses a promise to return the result. 245 246**Required permissions**: ohos.permission.WRITE_CALENDAR or ohos.permission.WRITE_WHOLE_CALENDAR 247 248**System capability**: SystemCapability.Applications.CalendarData 249 250**Parameters** 251 252| Name | Type | Mandatory| Description | 253| -------- | --------------------- | ---- | -------------- | 254| calendar | [Calendar](#calendar) | Yes | **Calendar** object to delete.| 255 256**Return value** 257 258| Type | Description | 259| -------------- | ------------------------- | 260| Promise\<void> | Promise that returns no value.| 261 262**Example** 263 264```typescript 265import { BusinessError } from '@ohos.base'; 266import { calendarMgr } from '../entryability/EntryAbility'; 267 268const calendarAccount: calendarManager.CalendarAccount = { 269 name: 'DeleteMyCalendarByPromise', 270 type: calendarManager.CalendarType.LOCAL 271}; 272calendarMgr?.createCalendar(calendarAccount).then((data: calendarManager.Calendar) => { 273 console.info(`Succeeded in creating the calendar, data -> ${JSON.stringify(data)}`); 274 calendarMgr?.getCalendar(calendarAccount).then((data: calendarManager.Calendar) => { 275 console.info(`Succeeded in getting the calendar, data -> ${JSON.stringify(data)}`); 276 calendarMgr?.deleteCalendar(data).then(() => { 277 console.info("Succeeded in deleting the calendar"); 278 }).catch((err: BusinessError) => { 279 console.error(`Failed to delete calendar: err -> ${JSON.stringify(err)}`); 280 }); 281 }).catch((err: BusinessError) => { 282 console.error(`Failed to get the calendar: err -> ${JSON.stringify(err)}`); 283 }); 284}).catch((error: BusinessError) => { 285 console.error(`Failed to create calendar, error -> ${JSON.stringify(error)}`); 286}) 287``` 288 289### getCalendar 290 291getCalendar(callback: AsyncCallback\<Calendar>): void 292 293Obtains 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. 294 295**Required permissions**: ohos.permission.READ_CALENDAR or ohos.permission.READ_WHOLE_CALENDAR 296 297**System capability**: SystemCapability.Applications.CalendarData 298 299**Parameters** 300 301| Name | Type | Mandatory| Description | 302| -------- | ------------------------------------ | ---- | ------------------------------------ | 303| callback | AsyncCallback<[Calendar](#calendar)> | Yes | Callback used to return the obtained **Calendar** object.| 304 305**Example** 306 307```typescript 308import { BusinessError } from '@ohos.base'; 309import { calendarMgr } from '../entryability/EntryAbility'; 310 311let calendar : calendarManager.Calendar | undefined = undefined; 312calendarMgr?.getCalendar((err: BusinessError, data:calendarManager.Calendar) => { 313 if (err) { 314 console.error(`Failed to get the calendar, err -> ${JSON.stringify(err)}`); 315 } else { 316 console.info(`Succeeded in getting the calendar, data -> ${JSON.stringify(data)}`); 317 calendar = data; 318 } 319}); 320``` 321 322### getCalendar 323 324getCalendar(calendarAccount: CalendarAccount, callback: AsyncCallback\<Calendar>): void 325 326Obtains a specified **Calendar** object. This API uses an asynchronous callback to return the result. 327 328**Required permissions**: ohos.permission.READ_CALENDAR or ohos.permission.READ_WHOLE_CALENDAR 329 330**System capability**: SystemCapability.Applications.CalendarData 331 332**Parameters** 333 334| Name | Type | Mandatory| Description | 335| --------------- | ------------------------------------ | ---- | ------------------------------------ | 336| calendarAccount | [CalendarAccount](#calendaraccount) | Yes | Calendar account information. | 337| callback | AsyncCallback<[Calendar](#calendar)> | Yes | Callback used to return the obtained **Calendar** object.| 338 339**Example** 340 341```typescript 342import { BusinessError } from '@ohos.base'; 343import { calendarMgr } from '../entryability/EntryAbility'; 344 345let calendar : calendarManager.Calendar | undefined = undefined; 346const calendarAccount: calendarManager.CalendarAccount = { 347 name: 'MyCalendar', 348 type: calendarManager.CalendarType.LOCAL 349}; 350calendarMgr?.createCalendar(calendarAccount).then((data: calendarManager.Calendar) => { 351 console.info(`Succeeded in creating the calendar, data -> ${JSON.stringify(data)}`); 352 calendarMgr?.getCalendar(calendarAccount, (err: BusinessError, data: calendarManager.Calendar) => { 353 if (err) { 354 console.error(`Failed to get the calendar, err -> ${JSON.stringify(err)}`); 355 } else { 356 console.info(`Succeeded in getting the calendar data -> ${JSON.stringify(data)}`); 357 calendar = data; 358 } 359 }); 360}).catch((error: BusinessError) => { 361 console.error(`Failed to create calendar, error -> ${JSON.stringify(error)}`); 362}) 363``` 364 365### getCalendar 366 367getCalendar(calendarAccount?: CalendarAccount): Promise\<Calendar> 368 369Obtains the default **Calendar** object or a specified **Calendar** object. This API uses a promise to return the result. 370 371**Required permissions**: ohos.permission.READ_CALENDAR or ohos.permission.READ_WHOLE_CALENDAR 372 373**System capability**: SystemCapability.Applications.CalendarData 374 375**Parameters** 376 377| Name | Type | Mandatory| Description | 378| --------------- | ----------------------------------- | ---- | ------------------------------------------------------------ | 379| 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.| 380 381**Return value** 382 383| Type | Description | 384| ------------------------------ | --------------------------------------- | 385| Promise<[Calendar](#calendar)> | Promise used to return the obtained **Calendar** object.| 386 387**Example** 388 389```typescript 390import { BusinessError } from '@ohos.base'; 391import { calendarMgr } from '../entryability/EntryAbility'; 392 393let calendar : calendarManager.Calendar | undefined = undefined; 394calendarMgr?.getCalendar().then((data: calendarManager.Calendar) => { 395 console.info(`Succeeded in getting the calendar, data -> ${JSON.stringify(data)}`); 396 calendar = data; 397}).catch((err: BusinessError) => { 398 console.error(`Failed to get the calendar, err -> ${JSON.stringify(err)}`); 399}); 400``` 401 402### getAllCalendars 403 404getAllCalendars(callback: AsyncCallback\<Calendar[]>): void 405 406Obtains the created and default **Calendar** objects of the current application. This API uses an asynchronous callback to return the result. 407 408**Required permissions**: ohos.permission.READ_CALENDAR or ohos.permission.READ_WHOLE_CALENDAR 409 410**System capability**: SystemCapability.Applications.CalendarData 411 412**Parameters** 413 414| Name | Type | Mandatory| Description | 415| -------- | -------------------------------------- | ---- | ----------------------------------------- | 416| callback | AsyncCallback<[Calendar](#calendar)[]> | Yes | Callback used to return an array of obtained **Calendar** objects.| 417 418**Example** 419 420```typescript 421import { BusinessError } from '@ohos.base'; 422import { calendarMgr } from '../entryability/EntryAbility'; 423 424calendarMgr?.getAllCalendars((err: BusinessError, data: calendarManager.Calendar[]) => { 425 if (err) { 426 console.error(`Failed to get all calendars, err -> ${JSON.stringify(err)}`); 427 } else { 428 console.info(`Succeeded in getting all calendars, data -> ${JSON.stringify(data)}`); 429 data.forEach((calendar) => { 430 const account = calendar.getAccount(); 431 console.info(`account -> ${JSON.stringify(account)}`); 432 }) 433 } 434}); 435``` 436 437### getAllCalendars 438 439getAllCalendars(): Promise\<Calendar[]> 440 441Obtains the created and default **Calendar** objects of the current application. This API uses a promise to return the result. 442 443**Required permissions**: ohos.permission.READ_CALENDAR or ohos.permission.WRITE_WHOLE_CALENDAR 444 445**System capability**: SystemCapability.Applications.CalendarData 446 447**Return value** 448 449| Type | Description | 450| -------------------------------- | ------------------------------------------- | 451| Promise<[Calendar](#calendar)[]> | Promise used to return an array of obtained **Calendar** objects.| 452 453**Example** 454 455```typescript 456import { BusinessError } from '@ohos.base'; 457import { calendarMgr } from '../entryability/EntryAbility'; 458 459calendarMgr?.getAllCalendars().then((data: calendarManager.Calendar[]) => { 460 console.info(`Succeeded in getting all calendars, data -> ${JSON.stringify(data)}`); 461 data.forEach((calendar) => { 462 const account = calendar.getAccount(); 463 console.info(`account -> ${JSON.stringify(account)}`); 464 }) 465}).catch((err: BusinessError) => { 466 console.error(`Failed to get all calendars, err -> ${JSON.stringify(err)}`); 467}); 468``` 469 470## Calendar 471 472In the following API examples, you need to use [createCalendar()](#createcalendar) or [getCalendar()](#getcalendar) to obtain a **Calendar** object before calling related APIs. 473 474### Attributes 475 476**System capability**: SystemCapability.Applications.CalendarData 477 478| Name| Type | Read-only| Mandatory| Description | 479| ---- | ------ | ---- | ---- | -------- | 480| id | number | Yes | Yes | Calendar account ID.| 481 482### addEvent 483 484addEvent(event: Event, callback: AsyncCallback\<number>): void 485 486Creates an event, with no event ID specified in [Event](#event). This API uses an asynchronous callback to return the result. 487 488**System capability**: SystemCapability.Applications.CalendarData 489 490**Parameters** 491 492| Name | Type | Mandatory| Description | 493| -------- | ---------------------- | ---- | ---------------------- | 494| event | [Event](#event) | Yes | **Event** object. | 495| callback | AsyncCallback\<number> | Yes | Callback used to return the event ID.| 496 497**Example** 498 499```typescript 500import { BusinessError } from '@ohos.base'; 501import { calendarMgr } from '../entryability/EntryAbility'; 502 503let calendar : calendarManager.Calendar | undefined = undefined; 504const date = new Date(); 505const event: calendarManager.Event = { 506 type: calendarManager.EventType.NORMAL, 507 startTime: date.getTime(), 508 endTime: date.getTime() + 60 * 60 * 1000 509}; 510calendarMgr?.getCalendar().then((data: calendarManager.Calendar) => { 511 console.info(`Succeeded in getting the calendar, data -> ${JSON.stringify(data)}`); 512 calendar = data; 513 calendar.addEvent(event, (err: BusinessError, data: number): void => { 514 if (err) { 515 console.error(`Failed to addEvent, err -> ${JSON.stringify(err)}`); 516 } else { 517 console.info(`Succeeded in adding the event, id -> ${data}`); 518 } 519 }); 520}).catch((err: BusinessError) => { 521 console.error(`Failed to get the calendar, err -> ${JSON.stringify(err)}`); 522}); 523``` 524 525### addEvent 526 527addEvent(event: Event): Promise\<number> 528 529Creates an event, with no event ID specified in [Event](#event). This API uses a promise to return the result. 530 531**System capability**: SystemCapability.Applications.CalendarData 532 533**Parameters** 534 535| Name| Type | Mandatory| Description | 536| ------ | --------------- | ---- | ----------- | 537| event | [Event](#event) | Yes | **Event** object.| 538 539**Return value** 540 541| Type | Description | 542| ---------------- | --------------------------- | 543| Promise\<number> | Promise used to return the event ID.| 544 545**Example** 546 547```typescript 548import { BusinessError } from '@ohos.base'; 549import { calendarMgr } from '../entryability/EntryAbility'; 550 551let calendar : calendarManager.Calendar | undefined = undefined; 552const date = new Date(); 553const event: calendarManager.Event = { 554 type: calendarManager.EventType.NORMAL, 555 startTime: date.getTime(), 556 endTime: date.getTime() + 60 * 60 * 1000 557}; 558calendarMgr?.getCalendar((err: BusinessError, data:calendarManager.Calendar) => { 559 if (err) { 560 console.error(`Failed to get the calendar, err -> ${JSON.stringify(err)}`); 561 } else { 562 console.info(`Succeeded in getting the calendar, data -> ${JSON.stringify(data)}`); 563 calendar = data; 564 calendar.addEvent(event).then((data: number) => { 565 console.info(`Succeeded in adding the event, id -> ${data}`); 566 }).catch((err: BusinessError) => { 567 console.error(`Failed to addEvent, err -> ${JSON.stringify(err)}`); 568 }); 569 } 570}); 571``` 572 573### addEvents 574 575addEvents(events: Event[], callback: AsyncCallback\<void>): void 576 577Creates a batch of events, with no event ID specified in [Event](#event). This API uses an asynchronous callback to return the result. 578 579**System capability**: SystemCapability.Applications.CalendarData 580 581**Parameters** 582 583| Name | Type | Mandatory| Description | 584| -------- | -------------------- | ---- | --------------- | 585| events | [Event](#event)[] | Yes | Array of **Event** objects.| 586| callback | AsyncCallback\<void> | Yes | Callback used to return the result. | 587 588**Example** 589 590```typescript 591import { BusinessError } from '@ohos.base'; 592import { calendarMgr } from '../entryability/EntryAbility'; 593 594let calendar : calendarManager.Calendar | undefined = undefined; 595const date = new Date(); 596const events: calendarManager.Event[] = [ 597 { 598 type: calendarManager.EventType.NORMAL, 599 startTime: date.getTime(), 600 endTime: date.getTime() + 60 * 60 * 1000 601 }, 602 { 603 type: calendarManager.EventType.NORMAL, 604 startTime: date.getTime(), 605 endTime: date.getTime() + 60 * 60 * 1000 606 } 607]; 608calendarMgr?.getCalendar((err: BusinessError, data:calendarManager.Calendar) => { 609 if (err) { 610 console.error(`Failed to get the calendar, err -> ${JSON.stringify(err)}`); 611 } else { 612 console.info(`Succeeded in getting the calendar, data -> ${JSON.stringify(data)}`); 613 calendar = data; 614 calendar.addEvents(events, (err: BusinessError) => { 615 if (err) { 616 console.error(`Failed to add events, err -> ${JSON.stringify(err)}`); 617 } else { 618 console.info("Succeeded in adding the events"); 619 } 620 }); 621 } 622}); 623``` 624 625### addEvents 626 627addEvents(events: Event[]): Promise\<void> 628 629Creates a batch of events, with no event ID specified in [Event](#event). This API uses a promise to return the result. 630 631**System capability**: SystemCapability.Applications.CalendarData 632 633**Parameters** 634 635| Name| Type | Mandatory| Description | 636| ------ | ----------------- | ---- | --------------- | 637| events | [Event](#event)[] | Yes | Array of **Event** objects.| 638 639**Return value** 640 641| Type | Description | 642| -------------- | ------------------------- | 643| Promise\<void> | Promise that returns no value.| 644 645**Example** 646 647```typescript 648import { BusinessError } from '@ohos.base'; 649import { calendarMgr } from '../entryability/EntryAbility'; 650 651let calendar : calendarManager.Calendar | undefined = undefined; 652const date = new Date(); 653const events: calendarManager.Event[] = [ 654 { 655 type: calendarManager.EventType.NORMAL, 656 startTime: date.getTime(), 657 endTime: date.getTime() + 60 * 60 * 1000 658 }, 659 { 660 type: calendarManager.EventType.NORMAL, 661 startTime: date.getTime(), 662 endTime: date.getTime() + 60 * 60 * 1000 663 } 664]; 665calendarMgr?.getCalendar((err: BusinessError, data:calendarManager.Calendar) => { 666 if (err) { 667 console.error(`Failed to get the calendar: err -> ${JSON.stringify(err)}`); 668 } else { 669 console.info(`Succeeded in getting the calendar, data -> ${JSON.stringify(data)}`); 670 calendar = data; 671 calendar.addEvents(events).then(() => { 672 console.info("Succeeded in adding the events"); 673 }).catch((err: BusinessError) => { 674 console.error(`Failed to add event, err -> ${JSON.stringify(err)}`); 675 }); 676 } 677}); 678``` 679 680### deleteEvent 681 682deleteEvent(id: number, callback: AsyncCallback\<void>): void 683 684Deletes an event with the specified ID. This API uses an asynchronous callback to return the result. 685 686**System capability**: SystemCapability.Applications.CalendarData 687 688**Parameters** 689 690| Name | Type | Mandatory| Description | 691| -------- | -------------------- | ---- | ---------- | 692| id | number | Yes | Event ID. | 693| callback | AsyncCallback\<void> | Yes | Callback used to return the result.| 694 695**Example** 696 697```typescript 698import { BusinessError } from '@ohos.base'; 699import { calendarMgr } from '../entryability/EntryAbility'; 700 701let calendar : calendarManager.Calendar | undefined = undefined; 702let id: number = 0; 703const date = new Date(); 704const event: calendarManager.Event = { 705 type: calendarManager.EventType.NORMAL, 706 startTime: date.getTime(), 707 endTime: date.getTime() + 60 * 60 * 1000 708}; 709calendarMgr?.getCalendar(async (err: BusinessError, data:calendarManager.Calendar) => { 710 if (err) { 711 console.error(`Failed to get the calendar, err -> ${JSON.stringify(err)}`); 712 } else { 713 console.info(`Succeeded in getting the calendar, data -> ${JSON.stringify(data)}`); 714 calendar = data; 715 await calendar.addEvent(event).then((data: number) => { 716 console.info(`Succeeded in adding the event, id -> ${data}`); 717 id = data; 718 }).catch((err: BusinessError) => { 719 console.error(`Failed to add event, err -> ${JSON.stringify(err)}`); 720 }); 721 calendar.deleteEvent(id, (err: BusinessError) => { 722 if (err) { 723 console.error(`Failed to delete event, err -> ${JSON.stringify(err)}`); 724 } else { 725 console.info(`Succeeded to delete event, err -> ${JSON.stringify(err)}`); 726 } 727 }); 728 } 729}); 730``` 731 732### deleteEvent 733 734deleteEvent(id: number): Promise\<void> 735 736Deletes an event with the specified ID. This API uses a promise to return the result. 737 738**System capability**: SystemCapability.Applications.CalendarData 739 740**Parameters** 741 742| Name| Type | Mandatory| Description | 743| ------ | ------ | ---- | -------- | 744| id | number | Yes | Event ID.| 745 746**Return value** 747 748| Type | Description | 749| -------------- | ------------------------- | 750| Promise\<void> | Promise that returns no value.| 751 752**Example** 753 754```typescript 755import { BusinessError } from '@ohos.base'; 756import { calendarMgr } from '../entryability/EntryAbility'; 757 758let calendar : calendarManager.Calendar | undefined = undefined; 759let id: number = 0; 760const date = new Date(); 761const event: calendarManager.Event = { 762 type: calendarManager.EventType.NORMAL, 763 startTime: date.getTime(), 764 endTime: date.getTime() + 60 * 60 * 1000 765}; 766calendarMgr?.getCalendar(async (err: BusinessError, data:calendarManager.Calendar) => { 767 if (err) { 768 console.error(`Failed to get the calendar: err->${JSON.stringify(err)}`); 769 } else { 770 console.info(`Succeeded in getting the calendar data->${JSON.stringify(data)}`); 771 calendar = data; 772 await calendar.addEvent(event).then((data: number) => { 773 console.info(`Succeeded in adding the event, id -> ${data}`); 774 id = data; 775 }).catch((err: BusinessError) => { 776 console.error(`Failed to add event, err -> ${JSON.stringify(err)}`); 777 }); 778 calendar.deleteEvent(id).then(() => { 779 console.info("Succeeded to delete event"); 780 }).catch((err: BusinessError) => { 781 console.error("Failed to delete event"); 782 }); 783 } 784}); 785``` 786 787### deleteEvents 788 789deleteEvents(ids: number[], callback: AsyncCallback\<void>): void 790 791Deletes a batch of events with the specified IDs. This API uses an asynchronous callback to return the result. 792 793**System capability**: SystemCapability.Applications.CalendarData 794 795**Parameters** 796 797| Name | Type | Mandatory| Description | 798| -------- | -------------------- | ---- | ------------ | 799| ids | number[] | Yes | Array of event IDs.| 800| callback | AsyncCallback\<void> | Yes | Callback used to return the result. | 801 802**Example** 803 804```typescript 805import { BusinessError } from '@ohos.base'; 806import { calendarMgr } from '../entryability/EntryAbility'; 807 808let calendar : calendarManager.Calendar | undefined = undefined; 809let id1: number = 0; 810let id2: number = 0; 811const date = new Date(); 812const event1: calendarManager.Event = { 813 type: calendarManager.EventType.NORMAL, 814 startTime: date.getTime(), 815 endTime: date.getTime() + 60 * 60 * 1000 816}; 817const event2: calendarManager.Event = { 818 type: calendarManager.EventType.IMPORTANT, 819 startTime: date.getTime(), 820 endTime: date.getTime() + 60 * 60 * 1000 821}; 822calendarMgr?.getCalendar(async (err: BusinessError, data:calendarManager.Calendar) => { 823 if (err) { 824 console.error(`Failed to get the calendar, err -> ${JSON.stringify(err)}`); 825 } else { 826 console.info(`Succeeded in getting the calendar, data -> ${JSON.stringify(data)}`); 827 calendar = data; 828 await calendar.addEvent(event1).then((data: number) => { 829 console.info(`Succeeded in adding the event, id -> ${data}`); 830 id1 = data; 831 }).catch((err: BusinessError) => { 832 console.error(`Failed to add event, err -> ${JSON.stringify(err)}`); 833 }); 834 await calendar.addEvent(event2).then((data: number) => { 835 console.info(`Succeeded in adding the event, id -> ${data}`); 836 id2 = data; 837 }).catch((err: BusinessError) => { 838 console.error(`Failed to add event, err -> ${JSON.stringify(err)}`); 839 }); 840 calendar.deleteEvents([id1, id2], (err: BusinessError) => { 841 if (err) { 842 console.error("Failed to delete events"); 843 } else { 844 console.info("Succeeded to delete events"); 845 } 846 }); 847 } 848}); 849``` 850 851### deleteEvents 852 853deleteEvents(ids: number[]): Promise\<void> 854 855Deletes a batch of events with the specified IDs. This API uses a promise to return the result. 856 857**System capability**: SystemCapability.Applications.CalendarData 858 859**Parameters** 860 861| Name| Type | Mandatory| Description | 862| ------ | -------- | ---- | ------------ | 863| ids | number[] | Yes | Array of event IDs.| 864 865**Return value** 866 867| Type | Description | 868| -------------- | ------------------------- | 869| Promise\<void> | Promise that returns no value.| 870 871**Example** 872 873```typescript 874import { BusinessError } from '@ohos.base'; 875import { calendarMgr } from '../entryability/EntryAbility'; 876 877let calendar : calendarManager.Calendar | undefined = undefined; 878let id1: number = 0; 879let id2: number = 0; 880const date = new Date(); 881const event1: calendarManager.Event = { 882 type: calendarManager.EventType.NORMAL, 883 startTime: date.getTime(), 884 endTime: date.getTime() + 60 * 60 * 1000 885}; 886const event2: calendarManager.Event = { 887 type: calendarManager.EventType.IMPORTANT, 888 startTime: date.getTime(), 889 endTime: date.getTime() + 60 * 60 * 1000 890}; 891calendarMgr?.getCalendar(async (err: BusinessError, data:calendarManager.Calendar) => { 892 if (err) { 893 console.error(`Failed to get the calendar, err -> ${JSON.stringify(err)}`); 894 } else { 895 console.info(`Succeeded in getting the calendar, data -> ${JSON.stringify(data)}`); 896 calendar = data; 897 await calendar.addEvent(event1).then((data: number) => { 898 console.info(`Succeeded in adding the event, id -> ${data}`); 899 id1 = data; 900 }).catch((err: BusinessError) => { 901 console.error(`Failed to add event, err -> ${JSON.stringify(err)}`); 902 }); 903 await calendar.addEvent(event2).then((data: number) => { 904 console.info(`Succeeded in adding the event, id -> ${data}`); 905 id2 = data; 906 }).catch((err: BusinessError) => { 907 console.error(`Failed to add event, err -> ${JSON.stringify(err)}`); 908 }); 909 calendar.deleteEvents([id1, id2]).then(() => { 910 console.info("Succeeded to delete events"); 911 }).catch((err: BusinessError) => { 912 console.error("Failed to delete events"); 913 }); 914 } 915}); 916``` 917 918### updateEvent 919 920updateEvent(event: Event, callback: AsyncCallback\<void>): void 921 922Updates an event. This API uses an asynchronous callback to return the result. 923 924**System capability**: SystemCapability.Applications.CalendarData 925 926**Parameters** 927 928| Name | Type | Mandatory| Description | 929| -------- | -------------------- | ---- | ----------- | 930| event | [Event](#event) | Yes | **Event** object.| 931| callback | AsyncCallback\<void> | Yes | Callback used to return the result. | 932 933**Example** 934 935```typescript 936import { BusinessError } from '@ohos.base'; 937import { calendarMgr } from '../entryability/EntryAbility'; 938 939let calendar : calendarManager.Calendar | undefined = undefined; 940const date = new Date(); 941const oriEvent: calendarManager.Event = { 942 title: 'update', 943 type: calendarManager.EventType.NORMAL, 944 description: 'updateEventTest', 945 startTime: date.getTime(), 946 endTime: date.getTime() + 60 * 60 * 1000 947}; 948calendarMgr?.getCalendar(async (err: BusinessError, data:calendarManager.Calendar) => { 949 if (err) { 950 console.error(`Failed to get the calendar, err -> ${JSON.stringify(err)}`); 951 } else { 952 console.info(`Succeeded in getting the calendar, data -> ${JSON.stringify(data)}`); 953 calendar = data; 954 await calendar.addEvent(oriEvent).then((data: number) => { 955 console.info(`Succeeded in adding the event, id -> ${data}`); 956 oriEvent.id = data; 957 oriEvent.title = 'newUpdate'; 958 }).catch((err: BusinessError) => { 959 console.error(`Failed to add event, err -> ${JSON.stringify(err)}`); 960 }); 961 calendar.updateEvent(oriEvent, (err: BusinessError) => { 962 if (err) { 963 console.error(`Failed to update event, err -> ${JSON.stringify(err)}`); 964 } else { 965 console.info("Succeeded in updating the event"); 966 } 967 }); 968 } 969}); 970``` 971 972### updateEvent 973 974updateEvent(event: Event): Promise\<void> 975 976Updates an event. This API uses a promise to return the result. 977 978**System capability**: SystemCapability.Applications.CalendarData 979 980**Parameters** 981 982| Name| Type | Mandatory| Description | 983| ------ | --------------- | ---- | ----------- | 984| event | [Event](#event) | Yes | **Event** object.| 985 986**Return value** 987 988| Type | Description | 989| -------------- | ------------------------- | 990| Promise\<void> | Promise that returns no value.| 991 992**Example** 993 994```typescript 995import { BusinessError } from '@ohos.base'; 996import { calendarMgr } from '../entryability/EntryAbility'; 997 998let calendar : calendarManager.Calendar | undefined = undefined; 999const date = new Date(); 1000const oriEvent: calendarManager.Event = { 1001 title: 'update', 1002 type: calendarManager.EventType.NORMAL, 1003 description: 'updateEventTest', 1004 startTime: date.getTime(), 1005 endTime: date.getTime() + 60 * 60 * 1000 1006}; 1007calendarMgr?.getCalendar(async (err: BusinessError, data:calendarManager.Calendar) => { 1008 if (err) { 1009 console.error(`Failed to get the calendar, err -> ${JSON.stringify(err)}`); 1010 } else { 1011 console.info(`Succeeded in getting the calendar, data -> ${JSON.stringify(data)}`); 1012 calendar = data; 1013 await calendar.addEvent(oriEvent).then((data: number) => { 1014 console.info(`Succeeded in adding the event, id -> ${data}`); 1015 oriEvent.id = data; 1016 oriEvent.title = 'newUpdate'; 1017 }).catch((err: BusinessError) => { 1018 console.error(`Failed to add event, err -> ${JSON.stringify(err)}`); 1019 }); 1020 calendar.updateEvent(oriEvent).then(() => { 1021 console.info(`Succeeded in updating the event`); 1022 }).catch((err: BusinessError) => { 1023 console.error(`Failed to update event, err -> ${JSON.stringify(err)}`); 1024 }); 1025 } 1026}); 1027``` 1028 1029### getEvents 1030 1031getEvents(callback: AsyncCallback\<Event[]>): void 1032 1033Obtains all events in a calendar. This API uses an asynchronous callback to return the result. 1034 1035**System capability**: SystemCapability.Applications.CalendarData 1036 1037**Parameters** 1038 1039| Name | Type | Mandatory| Description | 1040| -------- | -------------------------------- | ---- | --------------------------------- | 1041| callback | AsyncCallback<[Event](#event)[]> | Yes | Callback used to return an array of events.| 1042 1043**Example** 1044 1045```typescript 1046import { BusinessError } from '@ohos.base'; 1047import { calendarMgr } from '../entryability/EntryAbility'; 1048 1049let calendar : calendarManager.Calendar | undefined = undefined; 1050calendarMgr?.getCalendar((err: BusinessError, data:calendarManager.Calendar) => { 1051 if (err) { 1052 console.error(`Failed to get the calendar: err -> ${JSON.stringify(err)}`); 1053 } else { 1054 console.info(`Succeeded in getting the calendar data -> ${JSON.stringify(data)}`); 1055 calendar = data; 1056 calendar.getEvents((err: BusinessError, data: calendarManager.Event[]) => { 1057 if (err) { 1058 console.error(`Failed to get the events, err -> ${JSON.stringify(err)}`); 1059 } else { 1060 console.info(`Succeeded in getting events, data -> ${JSON.stringify(data)}`); 1061 } 1062 }); 1063 } 1064}); 1065``` 1066 1067### getEvents 1068 1069getEvents(eventFilter: EventFilter, eventKey: (keyof Event)[], callback: AsyncCallback\<Event[]>):void 1070 1071Obtains all events in a calendar that match the filter criteria. This API uses an asynchronous callback to return the result. 1072 1073**System capability**: SystemCapability.Applications.CalendarData 1074 1075**Parameters** 1076 1077| Name | Type | Mandatory| Description | 1078| ----------- | -------------------------------- | ---- | --------------------------------- | 1079| eventFilter | [EventFilter](#eventfilter) | Yes | Filter criteria. | 1080| eventKey | (keyof [Event](#event))[] | Yes | Filter field. | 1081| callback | AsyncCallback<[Event](#event)[]> | Yes | Callback used to return an array of events.| 1082 1083**Example** 1084 1085```typescript 1086import { BusinessError } from '@ohos.base'; 1087import { calendarMgr } from '../entryability/EntryAbility'; 1088 1089let calendar : calendarManager.Calendar | undefined = undefined; 1090let id1: number = 0; 1091let id2: number = 0; 1092const date = new Date(); 1093const event1: calendarManager.Event = { 1094 type: calendarManager.EventType.NORMAL, 1095 startTime: date.getTime(), 1096 endTime: date.getTime() + 60 * 60 * 1000 1097}; 1098const event2: calendarManager.Event = { 1099 type: calendarManager.EventType.IMPORTANT, 1100 startTime: date.getTime(), 1101 endTime: date.getTime() + 60 * 60 * 1000 1102}; 1103calendarMgr?.getCalendar(async (err: BusinessError, data:calendarManager.Calendar) => { 1104 if (err) { 1105 console.error(`Failed to get the calendar, err -> ${JSON.stringify(err)}`); 1106 } else { 1107 console.info(`Succeeded in getting the calendar, data -> ${JSON.stringify(data)}`); 1108 calendar = data; 1109 await calendar.addEvent(event1).then((data: number) => { 1110 console.info(`Succeeded in adding the event, id -> ${data}`); 1111 }).catch((err: BusinessError) => { 1112 console.error(`Failed to add event, err -> ${JSON.stringify(err)}`); 1113 }); 1114 await calendar.addEvent(event2).then((data: number) => { 1115 console.info(`Succeeded in adding the event, id -> ${data}`); 1116 }).catch((err: BusinessError) => { 1117 console.error(`Failed to add event, err -> ${JSON.stringify(err)}`); 1118 }); 1119 const filter = calendarManager.EventFilter.filterById([id1, id2]); 1120 calendar.getEvents(filter, ['title', 'type', 'startTime', 'endTime'], (err: BusinessError, data: calendarManager.Event[]) => { 1121 if (err) { 1122 console.error(`Failed to get the events, err -> ${JSON.stringify(err)}`); 1123 } else { 1124 console.info(`Succeeded in getting events, data -> ${JSON.stringify(data)}`); 1125 } 1126 }); 1127 } 1128}); 1129``` 1130 1131### getEvents 1132 1133getEvents(eventFilter?: EventFilter, eventKey?: (keyof Event)[]): Promise\<Event[]> 1134 1135Obtains all events in a calendar that match the filter criteria. This API uses a promise to return the result. 1136 1137**System capability**: SystemCapability.Applications.CalendarData 1138 1139**Parameters** 1140 1141| Name | Type | Mandatory| Description | 1142| ----------- | --------------------------- | ---- | ---------- | 1143| eventFilter | [EventFilter](#eventfilter) | No | Filter criteria.| 1144| eventKey | (keyof [Event](#event))[] | No | Filter field.| 1145 1146**Return value** 1147 1148| Type | Description | 1149| -------------------------- | ----------------------------------- | 1150| Promise<[Event](#event)[]> | Promise used to return an array of events.| 1151 1152**Example** 1153 1154```typescript 1155import { BusinessError } from '@ohos.base'; 1156import { calendarMgr } from '../entryability/EntryAbility'; 1157 1158let calendar : calendarManager.Calendar | undefined = undefined; 1159const date = new Date(); 1160const event: calendarManager.Event = { 1161 title: 'MyEvent', 1162 type: calendarManager.EventType.IMPORTANT, 1163 startTime: date.getTime(), 1164 endTime: date.getTime() + 60 * 60 * 1000 1165}; 1166calendarMgr?.getCalendar(async (err: BusinessError, data:calendarManager.Calendar) => { 1167 if (err) { 1168 console.error(`Failed to get the calendar, err -> ${JSON.stringify(err)}`); 1169 } else { 1170 console.info(`Succeeded in getting the calendar, data -> ${JSON.stringify(data)}`); 1171 calendar = data; 1172 await calendar.addEvent(event).then((data: number) => { 1173 console.info(`Succeeded in adding the event, id -> ${data}`); 1174 }).catch((err: BusinessError) => { 1175 console.error(`Failed to add event, err -> ${JSON.stringify(err)}`); 1176 }); 1177 const filter = calendarManager.EventFilter.filterByTitle('MyEvent'); 1178 calendar.getEvents(filter).then((data: calendarManager.Event[]) => { 1179 console.info(`Succeeded in getting events, data -> ${JSON.stringify(data)}`); 1180 }).catch((err: BusinessError) => { 1181 console.error(`Failed to get the events, err -> ${JSON.stringify(err)}`); 1182 }); 1183 } 1184}); 1185``` 1186 1187### getConfig 1188 1189getConfig(): CalendarConfig 1190 1191Obtains the calendar configuration information. 1192 1193**System capability**: SystemCapability.Applications.CalendarData 1194 1195**Return value** 1196 1197| Type | Description | 1198| --------------------------------- | -------------- | 1199| [CalendarConfig](#calendarconfig) | Calendar configuration information.| 1200 1201**Example** 1202 1203```typescript 1204import { calendarMgr } from '../entryability/EntryAbility'; 1205import { BusinessError } from '@ohos.base'; 1206 1207let calendar : calendarManager.Calendar | undefined = undefined; 1208calendarMgr?.getCalendar((err: BusinessError, data:calendarManager.Calendar) => { 1209 if (err) { 1210 console.error(`Failed to get the calendar, err -> ${JSON.stringify(err)}`); 1211 } else { 1212 console.info(`Succeeded in getting the calendar, data -> ${JSON.stringify(data)}`); 1213 calendar = data; 1214 const config = calendar.getConfig(); 1215 console.info("get config success"); 1216 } 1217}); 1218``` 1219 1220### setConfig 1221 1222setConfig(config: CalendarConfig, callback: AsyncCallback\<void>): void 1223 1224Sets the calendar configuration information. This API uses an asynchronous callback to return the result. 1225 1226**System capability**: SystemCapability.Applications.CalendarData 1227 1228**Parameters** 1229 1230| Name | Type | Mandatory| Description | 1231| -------- | --------------------------------- | ---- | -------------- | 1232| config | [CalendarConfig](#calendarconfig) | Yes | Calendar configuration information.| 1233| callback | AsyncCallback\<void> | Yes | Callback used to return the result. | 1234 1235**Example** 1236 1237```typescript 1238import { BusinessError } from '@ohos.base'; 1239import { calendarMgr } from '../entryability/EntryAbility'; 1240 1241let calendar : calendarManager.Calendar | undefined = undefined; 1242const config: calendarManager.CalendarConfig = { 1243 enableReminder: true, 1244 color: '#aabbcc' 1245}; 1246calendarMgr?.getCalendar((err: BusinessError, data:calendarManager.Calendar) => { 1247 if (err) { 1248 console.error(`Failed to get the calendar, err -> ${JSON.stringify(err)}`); 1249 } else { 1250 console.info(`Succeeded in getting the calendar, data -> ${JSON.stringify(data)}`); 1251 calendar = data; 1252 calendar.setConfig(config, (err: BusinessError) => { 1253 if (err) { 1254 console.error(`Failed to set config, err -> ${JSON.stringify(err)}`); 1255 } else { 1256 console.info(`Succeeded in setting config, config -> ${JSON.stringify(config)}`); 1257 } 1258 }); 1259 } 1260}); 1261``` 1262 1263### setConfig 1264 1265setConfig(config: CalendarConfig): Promise\<void> 1266 1267Sets the calendar configuration information. This API uses a promise to return the result. 1268 1269**System capability**: SystemCapability.Applications.CalendarData 1270 1271**Parameters** 1272 1273| Name| Type | Mandatory| Description | 1274| ------ | --------------------------------- | ---- | -------------- | 1275| config | [CalendarConfig](#calendarconfig) | Yes | Calendar configuration information.| 1276 1277**Return value** 1278 1279| Type | Description | 1280| -------------- | ------------------------- | 1281| Promise\<void> | Promise that returns no value.| 1282 1283**Example** 1284 1285```typescript 1286import { BusinessError } from '@ohos.base'; 1287import { calendarMgr } from '../entryability/EntryAbility'; 1288 1289let calendar : calendarManager.Calendar | undefined = undefined; 1290const config: calendarManager.CalendarConfig = { 1291 enableReminder: true, 1292 color: '#aabbcc' 1293}; 1294calendarMgr?.getCalendar((err: BusinessError, data:calendarManager.Calendar) => { 1295 if (err) { 1296 console.error(`Failed to get the calendar, err -> ${JSON.stringify(err)}`); 1297 } else { 1298 console.info(`Succeeded in getting the calendar, data -> ${JSON.stringify(data)}`); 1299 calendar = data; 1300 calendar.setConfig(config).then(() => { 1301 console.info(`Succeeded in setting config, data->${JSON.stringify(config)}`); 1302 }).catch((err: BusinessError) => { 1303 console.error(`Failed to set config, err->${JSON.stringify(err)}`); 1304 }); 1305 } 1306}); 1307``` 1308 1309### getAccount 1310 1311getAccount(): CalendarAccount 1312 1313Obtains the calendar account information. 1314 1315**System capability**: SystemCapability.Applications.CalendarData 1316 1317**Return value** 1318 1319| Type | Description | 1320| ----------------------------------- | -------------- | 1321| [CalendarAccount](#calendaraccount) | Calendar account information.| 1322 1323**Example** 1324 1325```typescript 1326import { calendarMgr } from '../entryability/EntryAbility'; 1327import { BusinessError } from '@ohos.base'; 1328 1329let calendar : calendarManager.Calendar | undefined = undefined; 1330calendarMgr?.getCalendar((err: BusinessError, data:calendarManager.Calendar) => { 1331 if (err) { 1332 console.error(`Failed to get the calendar, err -> ${JSON.stringify(err)}`); 1333 } else { 1334 console.info(`Succeeded in getting the calendar, data -> ${JSON.stringify(data)}`); 1335 calendar = data; 1336 const account = calendar.getAccount(); 1337 console.info(`get account success, account -> ${JSON.stringify(account)}`); 1338 } 1339}); 1340``` 1341 1342## CalendarAccount 1343 1344Describes the calendar account information. 1345 1346**System capability**: SystemCapability.Applications.CalendarData 1347 1348| Name | Type | Read-only| Mandatory| Description | 1349| ----------- | ----------------------------- | ---- | ---- | -------------------------------------- | 1350| name | string | Yes | Yes | Account name. | 1351| type | [CalendarType](#calendartype) | No | Yes | Account type. | 1352| displayName | string | No | No | Display name of the account. If this parameter is not set, an empty string is used.| 1353 1354## CalendarConfig 1355 1356Describes the calendar configuration information. 1357 1358**System capability**: SystemCapability.Applications.CalendarData 1359 1360| Name | Type | Read-only| Mandatory| Description | 1361| -------------- | --------------------------------------------------- | ---- | ---- | ------------------------------------------------------------ | 1362| enableReminder | boolean | No | No | 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**.| 1363| color | string/number | No | No | Calendar color. If this parameter is not set, the default value **'#0A59F7'** is used. | 1364 1365## Event 1366 1367Describes an **Event** object, including the event title, start time, and end time. 1368 1369**System capability**: SystemCapability.Applications.CalendarData 1370 1371| Name | Type | Read-only| Mandatory| Description | 1372| -------------- | --------------------------------- | ---- | ---- | ------------------------------------------------------------ | 1373| id | number | No | No | Event ID. This parameter does not need to be set in the [addEvent()](#addevent) or [addEvents()](#addevents) API.| 1374| type | [EventType](#eventtype) | No | Yes | Event type. | 1375| title | string | No | No | Event title. If this parameter is not set, an empty string is used. | 1376| location | [Location](#location) | No | No | Event location. If this parameter is not set, the default null value is used. | 1377| startTime | number | No | Yes | Start time of the event. The value is a 13-digit timestamp. | 1378| endTime | number | No | Yes | End time of the event. The value is a 13-digit timestamp. | 1379| isAllDay | boolean | No | No | 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**.| 1380| attendee | [Attendee](#attendee)[] | No | No | Event attendee. If this parameter is not set, the default null value is used. | 1381| timeZone | string | No | No | Time zone of the event. If this parameter is not set, the current system time zone is used. You can call the [getTimeZone()]() API to obtain the current system time zone.| 1382| reminderTime | number[] | No | No | 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. The value can be negative. | 1383| recurrenceRule | [RecurrenceRule](#recurrencerule) | No | No | Recurrence rule of the event. If this parameter is not set, the value does not recur. | 1384| description | string | No | No | Event description. If this parameter is not set, an empty string is used. | 1385| service | [EventService](#eventservice) | No | No | Event service. If this parameter is not set, no service is available. | 1386 1387## CalendarType 1388 1389Enumerates the account types. 1390 1391**System capability**: SystemCapability.Applications.CalendarData 1392 1393| Name | Value | Description | 1394| ---------- | ------------ | -------------------- | 1395| LOCAL | 'local' | Local account. | 1396| EMAIL | 'email' | Email account. | 1397| BIRTHDAY | 'birthday' | Birthday account. | 1398| CALDAV | 'caldav' | CalDAV account.| 1399| SUBSCRIBED | 'subscribed' | Subscription account. | 1400 1401## Location 1402 1403Describes the event location. 1404 1405**System capability**: SystemCapability.Applications.CalendarData 1406 1407| Name | Type | Read-only| Mandatory| Description | 1408| --------- | ------ | ---- | ---- | ------------------------ | 1409| location | string | No | No | Location. The default value is an empty string.| 1410| longitude | number | No | No | Longitude of the location. The default value is **0**. | 1411| latitude | number | No | No | Latitude of the location. The default value is **0**. | 1412 1413## EventFilter 1414 1415Implements an event filter. 1416 1417You can use [filterById()](#filterbyid), [filterByTime()](#filterbytime), or [filterByTitle()](#filterbytitle) to obtain an event filter, and then pass the filter in [getEvents()](#getevents) for filtering. 1418 1419### filterById 1420 1421static filterById(ids: number[]): EventFilter 1422 1423Defines an event ID based filter. 1424 1425**System capability**: SystemCapability.Applications.CalendarData 1426 1427**Parameters** 1428 1429| Name| Type | Mandatory| Description | 1430| ------ | -------- | ---- | ------------ | 1431| ids | number[] | Yes | Array of event IDs.| 1432 1433**Return value** 1434 1435| Type | Description | 1436| --------------------------- | -------------------- | 1437| [EventFilter](#eventfilter) | **EventFilter** object.| 1438 1439**Example** 1440 1441```typescript 1442import { BusinessError } from '@ohos.base'; 1443import { calendarMgr } from '../entryability/EntryAbility'; 1444 1445let calendar : calendarManager.Calendar | undefined = undefined; 1446let id1: number = 0; 1447let id2: number = 0; 1448const date = new Date(); 1449const event1: calendarManager.Event = { 1450 type: calendarManager.EventType.NORMAL, 1451 startTime: date.getTime(), 1452 endTime: date.getTime() + 60 * 60 * 1000 1453}; 1454const event2: calendarManager.Event = { 1455 type: calendarManager.EventType.IMPORTANT, 1456 startTime: date.getTime(), 1457 endTime: date.getTime() + 60 * 60 * 1000 1458}; 1459calendarMgr?.getCalendar(async (err: BusinessError, data:calendarManager.Calendar) => { 1460 if (err) { 1461 console.error(`Failed to get the calendar, err -> ${JSON.stringify(err)}`); 1462 } else { 1463 console.info(`Succeeded in getting the calendar, data -> ${JSON.stringify(data)}`); 1464 calendar = data; 1465 await calendar.addEvent(event1).then((data: number) => { 1466 console.info(`Succeeded in adding the event, id -> ${data}`); 1467 id1 = data; 1468 }).catch((err: BusinessError) => { 1469 console.error(`Failed to add event, err -> ${JSON.stringify(err)}`); 1470 }); 1471 await calendar.addEvent(event2).then((data: number) => { 1472 console.info(`Succeeded in adding the event, id -> ${data}`); 1473 id2 = data; 1474 }).catch((err: BusinessError) => { 1475 console.error(`Failed to add event, err -> ${JSON.stringify(err)}`); 1476 }); 1477 const filter = calendarManager.EventFilter.filterById([id1, id2]); 1478 calendar.getEvents(filter).then((data: calendarManager.Event[]) => { 1479 console.info(`Succeeded in filtering by id, data -> ${JSON.stringify(data)}`); 1480 }).catch((err: BusinessError) => { 1481 console.error(`Failed to filter by id, err -> ${JSON.stringify(err)}`); 1482 }); 1483 } 1484}); 1485``` 1486 1487### filterByTime 1488 1489static filterByTime(start: number, end: number): EventFilter 1490 1491Defines an event time based filter. 1492 1493**System capability**: SystemCapability.Applications.CalendarData 1494 1495**Parameters** 1496 1497| Name| Type | Mandatory| Description | 1498| ------ | ------ | ---- | ---------- | 1499| start | number | Yes | Start time.| 1500| end | number | Yes | End time.| 1501 1502**Return value** 1503 1504| Type | Description | 1505| --------------------------- | -------------------- | 1506| [EventFilter](#eventfilter) | **EventFilter** object.| 1507 1508**Example** 1509 1510```typescript 1511import { BusinessError } from '@ohos.base'; 1512import { calendarMgr } from '../entryability/EntryAbility'; 1513 1514let calendar : calendarManager.Calendar | undefined = undefined; 1515const event1: calendarManager.Event = { 1516 type: calendarManager.EventType.NORMAL, 1517 startTime: 1686931200000, 1518 endTime: 1687017600000 1519}; 1520const event2: calendarManager.Event = { 1521 type: calendarManager.EventType.IMPORTANT, 1522 startTime: 1686931200000, 1523 endTime: 1687017600000 1524}; 1525calendarMgr?.getCalendar(async (err: BusinessError, data:calendarManager.Calendar) => { 1526 if (err) { 1527 console.error(`Failed to get the calendar, err -> ${JSON.stringify(err)}`); 1528 } else { 1529 console.info(`Succeeded in getting the calendar, data -> ${JSON.stringify(data)}`); 1530 calendar = data; 1531 await calendar.addEvent(event1).then((data: number) => { 1532 console.info(`Succeeded in adding the event, id -> ${data}`); 1533 }).catch((err: BusinessError) => { 1534 console.error(`Failed to add event, err -> ${JSON.stringify(err)}`); 1535 }); 1536 await calendar.addEvent(event2).then((data: number) => { 1537 console.info(`Succeeded in adding the event, id -> ${data}`); 1538 }).catch((err: BusinessError) => { 1539 console.error(`Failed to add event, err -> ${JSON.stringify(err)}`); 1540 }); 1541 const filter = calendarManager.EventFilter.filterByTime(1686931200000, 1687017600000); 1542 calendar.getEvents(filter).then((data: calendarManager.Event[]) => { 1543 console.info(`Succeeded in filtering by time, data -> ${JSON.stringify(data)}`); 1544 }).catch((err: BusinessError) => { 1545 console.error(`Failed to filter by time, err -> ${JSON.stringify(err)}`); 1546 }); 1547 } 1548}); 1549``` 1550 1551### filterByTitle 1552 1553static filterByTitle(title: string): EventFilter 1554 1555Defines an event title based filter. 1556 1557**System capability**: SystemCapability.Applications.CalendarData 1558 1559**Parameters** 1560 1561| Name| Type | Mandatory| Description | 1562| ------ | ------ | ---- | ---------- | 1563| title | string | Yes | Event title.| 1564 1565**Return value** 1566 1567| Type | Description | 1568| --------------------------- | -------------------- | 1569| [EventFilter](#eventfilter) | **EventFilter** object.| 1570 1571**Example** 1572 1573```typescript 1574import { BusinessError } from '@ohos.base'; 1575import { calendarMgr } from '../entryability/EntryAbility'; 1576 1577let calendar : calendarManager.Calendar | undefined = undefined; 1578const event: calendarManager.Event = { 1579 title: 'MyEvent', 1580 type: calendarManager.EventType.NORMAL, 1581 startTime: 1686931200000, 1582 endTime: 1687017600000 1583}; 1584calendarMgr?.getCalendar(async (err: BusinessError, data:calendarManager.Calendar) => { 1585 if (err) { 1586 console.error(`Failed to get the calendar, err -> ${JSON.stringify(err)}`); 1587 } else { 1588 console.info(`Succeeded in getting the calendar, data -> ${JSON.stringify(data)}`); 1589 calendar = data; 1590 await calendar.addEvent(event).then((data: number) => { 1591 console.info(`Succeeded in adding the event, id -> ${data}`); 1592 }).catch((err: BusinessError) => { 1593 console.error(`Failed to add event, err -> ${JSON.stringify(err)}`); 1594 }); 1595 const filter = calendarManager.EventFilter.filterByTitle('MyEvent'); 1596 calendar.getEvents(filter).then((data: calendarManager.Event[]) => { 1597 console.info(`Succeeded in filtering by title, data -> ${JSON.stringify(data)}`); 1598 }).catch((err: BusinessError) => { 1599 console.error(`Failed to filter by title, err -> ${JSON.stringify(err)}`); 1600 }); 1601 } 1602}); 1603``` 1604 1605## EventType 1606 1607Enumerates event types. 1608 1609**System capability**: SystemCapability.Applications.CalendarData 1610 1611| Name | Value | Description | 1612| --------- | ---- | -------------------- | 1613| NORMAL | 0 | Normal event. | 1614| IMPORTANT | 1 | Important event. This type of event supports countdown.| 1615 1616## RecurrenceRule 1617 1618Describes the event recurrence rule. 1619 1620**System capability**: SystemCapability.Applications.CalendarData 1621 1622| Name | Type | Read-only| Mandatory| Description | 1623| ------------------- | ------------------------------------------- | ---- | ---- | ------------------------------- | 1624| recurrenceFrequency | [RecurrenceFrequency](#recurrencefrequency) | No | Yes | Type of the event recurrence rule. | 1625| expire | number | No | No | End date of the recurrence period. If this parameter is not set, the default value **0** is used.| 1626 1627## RecurrenceFrequency 1628 1629Enumerates the types of the event recurrence rule. 1630 1631**System capability**: SystemCapability.Applications.CalendarData 1632 1633| Name | Value | Description | 1634| ------- | ---- | ---------- | 1635| YEARLY | 0 | Yearly.| 1636| MONTHLY | 1 | Monthly.| 1637| WEEKLY | 2 | Weekly.| 1638| DAILY | 3 | Daily.| 1639 1640## Attendee 1641 1642Describes the event attendee. 1643 1644**System capability**: SystemCapability.Applications.CalendarData 1645 1646| Name | Type | Read-only| Mandatory| Description | 1647| ----- | ------ | ---- | ---- | -------------- | 1648| name | string | No | Yes | Name of the attendee.| 1649| email | string | No | Yes | Email address of the attendee.| 1650 1651## EventService 1652 1653Describes the event service. 1654 1655**System capability**: SystemCapability.Applications.CalendarData 1656 1657| Name | Type | Read-only| Mandatory| Description | 1658| ----------- | --------------------------- | ---- | ---- | ------------------------------------- | 1659| type | [ServiceType](#servicetype) | No | Yes | Service type. | 1660| uri | string | No | Yes | URI of the service. It can be used to redirect the user to a page of another application.| 1661| description | string | No | No | Description of the service. If this parameter is not set, an empty string is used. | 1662 1663## ServiceType 1664 1665Enumerates the event service types. 1666 1667**System capability**: SystemCapability.Applications.CalendarData 1668 1669| Name | Value | Description | 1670| --------------- | ---------------- | ------------ | 1671| MEETING | 'Meeting' | Join a meeting. | 1672| WATCHING | 'Watching' | Watch a video. | 1673| REPAYMENT | 'Repayment' | Make a payment. | 1674| LIVE | 'Live' | Watch live TV. | 1675| SHOPPING | 'Shopping' | Go shopping. | 1676| TRIP | 'Trip' | View the trip. | 1677| CLASS | 'Class' | Join class. | 1678| SPORTS_EVENTS | 'SportsEvents' | Watch a sports event.| 1679| SPORTS_EXERCISE | 'SportsExercise' | Start exercising. | 1680