1# @ohos.calendarManager (日程管理能力) 2 3本模块提供日历与日程管理能力,包括日历和日程的创建、删除、修改、查询等。日历管理器[CalendarManager](#calendarmanager)用于管理日历[Calendar](#calendar)。日历[Calendar](#calendar)主要包含帐户信息[CalendarAccount](#calendaraccount)和配置信息[CalendarConfig](#calendarconfig)。日历Calendar与日程Event属于一对多关系,一个Calendar可以有多个Event,一个Event只属于一个Calendar。 4 5> **说明:** 6> 7> 本模块首批接口从API version 10开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 8 9 10## 导入模块 11 12```js 13import calendarManager from '@ohos.calendarManager'; 14``` 15 16## calendarManager.getCalendarManager 17 18getCalendarManager(context : Context): CalendarManager; 19 20根据上下文获取CalendarManager对象,用于管理日历。 21 22**系统能力**: SystemCapability.Applications.CalendarData 23 24**模型约束**:此接口仅可在Stage模型下使用。 25 26**参数**: 27 28| 参数名 | 类型 | 必填 | 说明 | 29| -------- | --------------------------- | ---- | ------------------------------------------------------------ | 30| context | Context | 是 | 应用上下文Context,Stage模型的应用Context定义见[Context](js-apis-inner-application-context.md)。 | 31 32 33**示例**: 34 35```ts 36 // 获取context 37 // 以下代码中的class EntryAbility extends UIAbility,onWindowStageCreate在工程main/ets/entryability/EntryAbility.ets中,测试ohosTest/ets/testability/TestAbility.ets中有,可直接使用 38 import UIAbility from '@ohos.app.ability.UIAbility'; 39 import common from '@ohos.app.ability.common'; 40 41 export let mContext : common.UIAbilityContext | null = null; 42 class EntryAbility extends UIAbility { 43 onWindowStageCreate(windowStage: window.WindowStage){ 44 mContext = this.context; 45 } 46 } 47 let calendarMgr:calendarManager.CalendarManager = calendarManager.getCalendarManager(mContext as Context); 48``` 49 50## CalendarManager 51 52下列API示例中需先通过[getCalendarManager()](#calendarmanagergetcalendarmanager)方法获取CalendarManager对象,再通过此对象调用对应方法,进行Calendar的创建、删除、修改、查询等操作。 53 54 55### createCalendar 56 57createCalendar(calendarAccount: CalendarAccount, callback: AsyncCallback\<Calendar>): void 58 59根据日历帐户信息,创建一个Calendar对象,使用callback异步回调。 60 61**需要权限**: ohos.permission.WRITE_CALENDAR or ohos.permission.WRITE_WHOLE_CALENDAR 62 63**系统能力**: SystemCapability.Applications.CalendarData 64 65**参数**: 66 67| 参数名 | 类型 | 必填 | 说明 | 68| --------------- | ------------------------------------- | ---- | ---------------------------------- | 69| calendarAccount | [CalendarAccount](#calendaraccount) | 是 | 日历帐户信息。 | 70| callback | AsyncCallback\<[Calendar](#calendar)> | 是 | 回调函数,返回创建的Calendar对象。 | 71 72**示例**: 73 74```typescript 75import { BusinessError } from '@ohos.base'; 76 77let calendar : calendarManager.Calendar | undefined = undefined; 78const calendarAccount: calendarManager.CalendarAccount = { 79 name: 'MyCalendar', 80 type: calendarManager.CalendarType.LOCAL 81}; 82try { 83 calendarMgr.createCalendar(calendarAccount, (err: BusinessError, data: calendarManager.Calendar) => { 84 if (err) { 85 console.error(`Failed to create calendar: err->${JSON.stringify(err)}`); 86 } else { 87 console.info(`Succeeded in creating calendar data->${JSON.stringify(data)}`); 88 calendar = data; 89 } 90 }); 91} catch (error) { 92 console.error(`Failed to create calendar: err->${JSON.stringify(error)}`); 93} 94``` 95 96### createCalendar 97 98createCalendar(calendarAccount: CalendarAccount): Promise\<Calendar> 99 100根据日历帐户信息,创建一个Calendar对象,使用Promise异步回调。 101 102**需要权限**: ohos.permission.WRITE_CALENDAR or ohos.permission.WRITE_WHOLE_CALENDAR 103 104**系统能力**: SystemCapability.Applications.CalendarData 105 106**参数**: 107 108| 参数名 | 类型 | 必填 | 说明 | 109| --------------- | ----------------------------------- | ---- | -------------- | 110| calendarAccount | [CalendarAccount](#calendaraccount) | 是 | 日历帐户信息。 | 111 112**返回值**: 113 114| 类型 | 说明 | 115| ------------------------------ | ------------------------------------- | 116| Promise<[Calendar](#calendar)> | Promise对象,返回创建的Calendar对象。 | 117 118**示例**: 119 120```ts 121import { BusinessError } from '@ohos.base'; 122 123let calendar : calendarManager.Calendar | undefined = undefined; 124const calendarAccount: calendarManager.CalendarAccount = { 125 name: 'MyCalendar', 126 type: calendarManager.CalendarType.LOCAL, 127 displayName : 'MyApplication' 128}; 129calendarMgr.createCalendar(calendarAccount).then((data: calendarManager.Calendar) => { 130 console.info(`Succeeded in creating calendar data->${JSON.stringify(data)}`); 131 calendar = data; 132}).catch((error : BusinessError) => { 133 console.error(`Failed to create calendar: err->${JSON.stringify(error)}`); 134}); 135``` 136 137### deleteCalendar 138 139deleteCalendar(calendar: Calendar, callback: AsyncCallback\<void>): void 140 141删除指定Calendar对象,使用callback异步回调。 142 143**需要权限**: ohos.permission.WRITE_CALENDAR or ohos.permission.WRITE_WHOLE_CALENDAR 144 145**系统能力**: SystemCapability.Applications.CalendarData 146 147**参数**: 148 149| 参数名 | 类型 | 必填 | 说明 | 150| -------- | --------------------- | ---- | -------------- | 151| calendar | [Calendar](#calendar) | 是 | Calendar对象。 | 152| callback | AsyncCallback\<void> | 是 | 回调函数。 | 153 154**示例**: 155 156```typescript 157import { BusinessError } from '@ohos.base'; 158 159const calendarAccount: calendarManager.CalendarAccount = { 160 name: 'MyCalendar', 161 type: calendarManager.CalendarType.LOCAL 162}; 163calendarMgr.getCalendar(calendarAccount, (err: BusinessError, data: calendarManager.Calendar) => { 164 if (err) { 165 console.error(`Failed to get calendar: err->${JSON.stringify(err)}`); 166 } else { 167 console.info("Succeeded in getting calendar"); 168 calendarMgr.deleteCalendar(data, (err: BusinessError) => { 169 if (err) { 170 console.error(`Failed to delete calendar: err->${JSON.stringify(err)}`); 171 } else { 172 console.info("Succeeded in deleting calendar"); 173 } 174 }); 175 } 176}); 177``` 178 179### deleteCalendar 180 181deleteCalendar(calendar: Calendar): Promise\<void> 182 183删除指定Calendar对象,使用Promise异步回调。 184 185**需要权限**: ohos.permission.WRITE_CALENDAR or ohos.permission.WRITE_WHOLE_CALENDAR 186 187**系统能力**: SystemCapability.Applications.CalendarData 188 189**参数**: 190 191| 参数名 | 类型 | 必填 | 说明 | 192| -------- | --------------------- | ---- | -------------- | 193| calendar | [Calendar](#calendar) | 是 | Calendar对象。 | 194 195**返回值**: 196 197| 类型 | 说明 | 198| -------------- | ------------------------- | 199| Promise\<void> | 无返回结果的Promise对象。 | 200 201**示例**: 202 203```typescript 204import { BusinessError } from '@ohos.base'; 205 206const calendarAccount: calendarManager.CalendarAccount = { 207 name: 'MyCalendar', 208 type: calendarManager.CalendarType.LOCAL 209}; 210calendarMgr.getCalendar(calendarAccount).then((data: calendarManager.Calendar) => { 211 console.info("Succeeded in getting calendar"); 212 calendarMgr.deleteCalendar(data).then(() => { 213 console.info("Succeeded in deleting calendar"); 214 }).catch((err: BusinessError) => { 215 console.error(`Failed to delete calendar: err->${JSON.stringify(err)}`); 216 }); 217}).catch((err: BusinessError) => { 218 console.error(`Failed to get calendar: err->${JSON.stringify(err)}`); 219}); 220``` 221 222### getCalendar 223 224getCalendar(callback: AsyncCallback\<Calendar>): void 225 226获取默认Calendar对象,默认Calendar是日历存储首次运行时创建的,若创建Event时不关注其Calendar归属,则无须通过[createCalendar()](#createcalendar)创建Calendar,直接使用默认Calendar,使用callback异步回调。 227 228**需要权限**:ohos.permission.READ_CALENDAR or ohos.permission.READ_WHOLE_CALENDAR 229 230**系统能力**: SystemCapability.Applications.CalendarData 231 232**参数**: 233 234| 参数名 | 类型 | 必填 | 说明 | 235| -------- | ------------------------------------ | ---- | ------------------------------------ | 236| callback | AsyncCallback<[Calendar](#calendar)> | 是 | 回调函数,返回查询到的Calendar对象。 | 237 238**示例**: 239 240```ts 241import { BusinessError } from '@ohos.base'; 242 243let calendar : calendarManager.Calendar | undefined = undefined; 244calendarMgr.getCalendar((err: BusinessError, data:calendarManager.Calendar) => { 245 if (err) { 246 console.error(`Failed to get calendar: err->${JSON.stringify(err)}`); 247 } else { 248 console.info(`Succeeded in getting calendar data->${JSON.stringify(data)}`); 249 calendar = data; 250 } 251}); 252``` 253 254### getCalendar 255 256getCalendar(calendarAccount: CalendarAccount, callback: AsyncCallback\<Calendar>): void 257 258获取指定Calendar对象,使用callback异步回调。 259 260**需要权限**: ohos.permission.READ_CALENDAR or ohos.permission.READ_WHOLE_CALENDAR 261 262**系统能力**: SystemCapability.Applications.CalendarData 263 264**参数**: 265 266| 参数名 | 类型 | 必填 | 说明 | 267| --------------- | ------------------------------------ | ---- | ------------------------------------ | 268| calendarAccount | [CalendarAccount](#calendaraccount) | 是 | 日历帐户信息。 | 269| callback | AsyncCallback<[Calendar](#calendar)> | 是 | 回调函数,返回查询到的Calendar对象。 | 270 271**示例**: 272 273```ts 274import { BusinessError } from '@ohos.base'; 275 276let calendar : calendarManager.Calendar | undefined = undefined; 277const calendarAccount: calendarManager.CalendarAccount = { 278 name: 'MyCalendar', 279 type: calendarManager.CalendarType.LOCAL 280}; 281calendarMgr.getCalendar(calendarAccount, (err: BusinessError, data: calendarManager.Calendar) => { 282 if (err) { 283 console.error(`Failed to get calendar: err->${JSON.stringify(err)}`); 284 } else { 285 console.info(`Succeeded in getting calendar data->${JSON.stringify(data)}`); 286 calendar = data; 287 } 288}); 289``` 290 291### getCalendar 292 293getCalendar(calendarAccount?: CalendarAccount): Promise\<Calendar> 294 295获取默认Calendar对象或者指定Calendar对象,使用Promise异步回调。 296 297**需要权限**: ohos.permission.READ_CALENDAR or ohos.permission.READ_WHOLE_CALENDAR 298 299**系统能力**: SystemCapability.Applications.CalendarData 300 301**参数**: 302 303| 参数名 | 类型 | 必填 | 说明 | 304| --------------- | ----------------------------------- | ---- | ------------------------------------------------------------ | 305| calendarAccount | [CalendarAccount](#calendaraccount) | 否 | 日历帐户信息,用来获取指定Calendar对象,不填时,表示获取默认Calendar对象。 | 306 307**返回值**: 308 309| 类型 | 说明 | 310| ------------------------------ | --------------------------------------- | 311| Promise<[Calendar](#calendar)> | Promise对象,返回查询到的Calendar对象。 | 312 313**示例**: 314 315```ts 316import { BusinessError } from '@ohos.base'; 317 318let calendar : calendarManager.Calendar | undefined = undefined; 319calendarMgr.getCalendar().then((data: calendarManager.Calendar) => { 320 console.info(`Succeeded in getting calendar data->${JSON.stringify(data)}`); 321 calendar = data; 322}).catch((err: BusinessError) => { 323 console.error(`Failed to get calendar: err->${JSON.stringify(err)}`); 324}); 325``` 326 327### getAllCalendars 328 329getAllCalendars(callback: AsyncCallback\<Calendar[]>): void 330 331获取当前应用所有创建的Calendar对象以及默认Calendar对象,使用callback异步回调。 332 333**需要权限**:ohos.permission.READ_CALENDAR or ohos.permission.READ_WHOLE_CALENDAR 334 335**系统能力**: SystemCapability.Applications.CalendarData 336 337**参数**: 338 339| 参数名 | 类型 | 必填 | 说明 | 340| -------- | -------------------------------------- | ---- | ----------------------------------------- | 341| callback | AsyncCallback<[Calendar](#calendar)[]> | 是 | 回调函数, 返回查询到的Calendar对象数组。 | 342 343**示例**: 344 345```ts 346import { BusinessError } from '@ohos.base'; 347 348calendarMgr.getAllCalendars((err: BusinessError, data: calendarManager.Calendar[]) => { 349 if (err) { 350 console.error(`Failed to get all calendars: err->${JSON.stringify(err)}`); 351 } else { 352 console.info(`Succeeded in getting all calendars->${JSON.stringify(data)}`); 353 data.forEach((calendar) => { 354 const account = calendar.getAccount(); 355 console.info(`account->${JSON.stringify(account)}`); 356 }) 357 } 358}); 359``` 360 361### getAllCalendars 362 363getAllCalendars(): Promise\<Calendar[]> 364 365获取当前应用所有创建的Calendar对象以及默认Calendar对象,使用Promise异步回调。 366 367**需要权限**: ohos.permission.READ_CALENDAR or ohos.permission.WRITE_WHOLE_CALENDAR 368 369**系统能力**: SystemCapability.Applications.CalendarData 370 371**返回值**: 372 373| 类型 | 说明 | 374| -------------------------------- | ------------------------------------------- | 375| Promise<[Calendar](#calendar)[]> | Promise对象,返回查询到的Calendar对象数组。 | 376 377**示例**: 378 379```ts 380import { BusinessError } from '@ohos.base'; 381 382calendarMgr.getAllCalendars().then((data: calendarManager.Calendar[]) => { 383 console.info(`Succeeded in getting all calendars->${JSON.stringify(data)}`); 384 data.forEach((calendar) => { 385 const account = calendar.getAccount(); 386 console.info(`account->${JSON.stringify(account)}`); 387 }) 388}).catch((err: BusinessError) => { 389 console.error(`Failed to get all calendars: err->${JSON.stringify(err)}`); 390}); 391``` 392 393## Calendar 394 395下列API示例中需先通过[createCalendar()](#createcalendar)、[getCalendar()](#getcalendar)中任一方法获取Calendar对象,再通过此对象调用对应方法,对该Calendar下的日程进行创建、删除、修改、查询等操作。 396 397### 属性 398 399**系统能力**:SystemCapability.Applications.CalendarData 400 401| 名称 | 类型 | 只读 | 必填 | 说明 | 402| ---- | ------ | ---- | ---- | -------- | 403| id | number | 是 | 是 | 帐户id。 | 404 405### addEvent 406 407addEvent(event: Event, callback: AsyncCallback\<number>): void 408 409创建日程,入参[Event](#event)不填日程id,使用callback异步回调。 410 411**系统能力**: SystemCapability.Applications.CalendarData 412 413**参数**: 414 415| 参数名 | 类型 | 必填 | 说明 | 416| -------- | ---------------------- | ---- | ---------------------- | 417| event | [Event](#event) | 是 | Event对象。 | 418| callback | AsyncCallback\<number> | 是 | 回调函数,返回日程id。 | 419 420**示例**: 421 422```ts 423import { BusinessError } from '@ohos.base'; 424 425const date = new Date(); 426const event: calendarManager.Event = { 427 type: calendarManager.EventType.NORMAL, 428 startTime: date.getTime(), 429 endTime: date.getTime() + 60 * 60 * 1000 430}; 431calendar.addEvent(event, (err: BusinessError, data: number): void => { 432 if (err) { 433 console.error(`Failed to addEvent: err->${JSON.stringify(err)}`); 434 } else { 435 console.info(`Succeeded in adding event id:${data}`); 436 } 437}); 438``` 439 440### addEvent 441 442addEvent(event: Event): Promise\<number> 443 444创建日程,入参[Event](#event)不填日程id,使用Promise异步回调。 445 446**系统能力**: SystemCapability.Applications.CalendarData 447 448**参数**: 449 450| 参数名 | 类型 | 必填 | 说明 | 451| ------ | --------------- | ---- | ----------- | 452| event | [Event](#event) | 是 | Event对象。 | 453 454**返回值**: 455 456| 类型 | 说明 | 457| ---------------- | --------------------------- | 458| Promise\<number> | Promise对象,返回日程的id。 | 459 460**示例**: 461 462```ts 463import { BusinessError } from '@ohos.base'; 464 465const date = new Date(); 466const event: calendarManager.Event = { 467 type: calendarManager.EventType.NORMAL, 468 startTime: date.getTime(), 469 endTime: date.getTime() + 60 * 60 * 1000 470}; 471calendar.addEvent(event).then((data: number) => { 472 console.info(`Succeeded in adding event id:${data}`); 473}).catch((err: BusinessError) => { 474 console.error(`Failed to addEvent: err->${JSON.stringify(err)}`); 475}); 476``` 477 478### addEvents 479 480addEvents(events: Event[], callback: AsyncCallback\<void>): void 481 482批量创建日程,入参[Event](#event)不填日程id,使用callback异步回调。 483 484**系统能力**: SystemCapability.Applications.CalendarData 485 486**参数**: 487 488| 参数名 | 类型 | 必填 | 说明 | 489| -------- | -------------------- | ---- | --------------- | 490| events | [Event](#event)[] | 是 | Event对象数组。 | 491| callback | AsyncCallback\<void> | 是 | 回调函数。 | 492 493**示例**: 494 495```ts 496import { BusinessError } from '@ohos.base'; 497 498const date = new Date(); 499const events: calendarManager.Event[] = [ 500 { 501 type: calendarManager.EventType.NORMAL, 502 startTime: date.getTime(), 503 endTime: date.getTime() + 60 * 60 * 1000 504 }, 505 { 506 type: calendarManager.EventType.NORMAL, 507 startTime: date.getTime(), 508 endTime: date.getTime() + 60 * 60 * 1000 509 } 510]; 511calendar.addEvents(events, (err: BusinessError) => { 512 if (err) { 513 console.error(`Failed to addEvent: err->${JSON.stringify(err)}`); 514 } else { 515 console.info("Succeeded in adding events"); 516 } 517}); 518``` 519 520### addEvents 521 522addEvents(events: Event[]): Promise\<void> 523 524批量创建日程,入参[Event](#event)不填日程id,使用Promise异步回调。 525 526**系统能力**: SystemCapability.Applications.CalendarData 527 528**参数**: 529 530| 参数名 | 类型 | 必填 | 说明 | 531| ------ | ----------------- | ---- | --------------- | 532| events | [Event](#event)[] | 是 | Event对象数组。 | 533 534**返回值**: 535 536| 类型 | 说明 | 537| -------------- | ------------------------- | 538| Promise\<void> | 无返回结果的Promise对象。 | 539 540**示例**: 541 542```ts 543import { BusinessError } from '@ohos.base'; 544 545const date = new Date(); 546const events: calendarManager.Event[] = [ 547 { 548 type: calendarManager.EventType.NORMAL, 549 startTime: date.getTime(), 550 endTime: date.getTime() + 60 * 60 * 1000 551 }, 552 { 553 type: calendarManager.EventType.NORMAL, 554 startTime: date.getTime(), 555 endTime: date.getTime() + 60 * 60 * 1000 556 } 557]; 558calendar.addEvents(events).then(() => { 559 console.info("Succeeded in adding events"); 560}).catch((err: BusinessError) => { 561 console.error(`Failed to addEvent: err->${JSON.stringify(err)}`); 562}); 563``` 564 565### deleteEvent 566 567deleteEvent(id: number, callback: AsyncCallback\<void>): void 568 569删除指定id的日程,使用callback异步回调。 570 571**系统能力**: SystemCapability.Applications.CalendarData 572 573**参数**: 574 575| 参数名 | 类型 | 必填 | 说明 | 576| -------- | -------------------- | ---- | ---------- | 577| id | number | 是 | 日程id。 | 578| callback | AsyncCallback\<void> | 是 | 回调函数。 | 579 580**示例**: 581 582```ts 583import { BusinessError } from '@ohos.base'; 584 585calendar.deleteEvent(1, (err: BusinessError) => { 586 if (err) { 587 console.error("Failed to delete event"); 588 } else { 589 console.info("Succeeded in deleting event"); 590 } 591}); 592``` 593 594### deleteEvent 595 596deleteEvent(id: number): Promise\<void> 597 598删除指定id的日程,使用Promise异步回调。 599 600**系统能力**: SystemCapability.Applications.CalendarData 601 602**参数**: 603 604| 参数名 | 类型 | 必填 | 说明 | 605| ------ | ------ | ---- | -------- | 606| id | number | 是 | 日程id。 | 607 608**返回值**: 609 610| 类型 | 说明 | 611| -------------- | ------------------------- | 612| Promise\<void> | 无返回结果的Promise对象。 | 613 614**示例**: 615 616```ts 617import { BusinessError } from '@ohos.base'; 618 619calendar.deleteEvent(1).then(() => { 620 console.info("Succeeded in deleting event"); 621}).catch((err: BusinessError) => { 622 console.error("Failed to delete event"); 623}); 624``` 625 626### deleteEvents 627 628deleteEvents(ids: number[], callback: AsyncCallback\<void>): void 629 630根据日程id,批量删除日程,使用callback异步回调。 631 632**系统能力**: SystemCapability.Applications.CalendarData 633 634**参数**: 635 636| 参数名 | 类型 | 必填 | 说明 | 637| -------- | -------------------- | ---- | ------------ | 638| ids | number[] | 是 | 日程id数组。 | 639| callback | AsyncCallback\<void> | 是 | 回调函数。 | 640 641**示例**: 642 643```ts 644import { BusinessError } from '@ohos.base'; 645 646calendar.deleteEvents([1, 2], (err: BusinessError) => { 647 if (err) { 648 console.error("Failed to delete events"); 649 } else { 650 console.info("Succeeded in deleting events"); 651 } 652}); 653``` 654 655### deleteEvents 656 657deleteEvents(ids: number[]): Promise\<void> 658 659根据日程id,批量删除日程,使用Promise异步回调。 660 661**系统能力**: SystemCapability.Applications.CalendarData 662 663**参数**: 664 665| 参数名 | 类型 | 必填 | 说明 | 666| ------ | -------- | ---- | ------------ | 667| ids | number[] | 是 | 日程id数组。 | 668 669**返回值**: 670 671| 类型 | 说明 | 672| -------------- | ------------------------- | 673| Promise\<void> | 无返回结果的Promise对象。 | 674 675**示例**: 676 677```ts 678import { BusinessError } from '@ohos.base'; 679 680calendar.deleteEvents([1, 2]).then(() => { 681 console.info("Succeeded in deleting events"); 682}).catch((err: BusinessError) => { 683 console.error("Failed to delete events"); 684}); 685``` 686 687### updateEvent 688 689updateEvent(event: Event, callback: AsyncCallback\<void>): void 690 691更新日程,使用callback异步回调。 692 693**系统能力**: SystemCapability.Applications.CalendarData 694 695**参数**: 696 697| 参数名 | 类型 | 必填 | 说明 | 698| -------- | -------------------- | ---- | ----------- | 699| event | [Event](#event) | 是 | Event对象。 | 700| callback | AsyncCallback\<void> | 是 | 回调函数。 | 701 702**示例**: 703 704```ts 705import { BusinessError } from '@ohos.base'; 706 707const date = new Date(); 708const oriEvent: calendarManager.Event = { 709 title: 'update', 710 type: calendarManager.EventType.NORMAL, 711 description: 'updateEventTest', 712 startTime: date.getTime(), 713 endTime: date.getTime() + 60 * 60 * 1000 714}; 715calendar.addEvent(oriEvent, (err: BusinessError, data: number): void => { 716 if (err) { 717 console.error(`Failed to addEvent: err->${JSON.stringify(err)}`); 718 } else { 719 console.info(`Succeeded in adding event id:${data}`); 720 oriEvent.id = data; // must set id 721 oriEvent.title = 'newUpdate'; 722 calendar.updateEvent(oriEvent, (err: BusinessError) => { 723 if (err) { 724 console.error(`Failed to updateEvent: err->${JSON.stringify(err)}`); 725 } else { 726 console.info("Succeeded in updating event"); 727 } 728 }); 729 } 730}); 731``` 732 733### updateEvent 734 735updateEvent(event: Event): Promise\<void> 736 737更新日程,使用Promise异步回调。 738 739**系统能力**: SystemCapability.Applications.CalendarData 740 741**参数**: 742 743| 参数名 | 类型 | 必填 | 说明 | 744| ------ | --------------- | ---- | ----------- | 745| event | [Event](#event) | 是 | Event对象。 | 746 747**返回值**: 748 749| 类型 | 说明 | 750| -------------- | ------------------------- | 751| Promise\<void> | 无返回结果的Promise对象。 | 752 753**示例**: 754 755```ts 756import { BusinessError } from '@ohos.base'; 757 758const filter = calendarManager.EventFilter.filterByTitle('update'); 759calendar.getEvents(filter).then((events : calendarManager.Event[]) => { 760 console.info(`Succeeded in getEvents`); 761 if (events.length < 1) { 762 return; 763 } 764 let newEvent = events[0]; 765 newEvent.title = 'newUpdate'; 766 calendar.updateEvent(newEvent).then(() => { 767 console.info(`Succeeded in updateEvent`); 768 }).catch((err: BusinessError) => { 769 console.error(`Failed to updateEvent err->${JSON.stringify(err)}`); 770 }); 771}).catch((err: BusinessError) => { 772 console.error(`Failed to getEvents err->${JSON.stringify(err)}`); 773}); 774``` 775 776### getEvents 777 778getEvents(callback: AsyncCallback\<Event[]>): void 779 780查询Calendar下所有Event,使用callback异步回调。 781 782**系统能力**: SystemCapability.Applications.CalendarData 783 784**参数**: 785 786| 参数名 | 类型 | 必填 | 说明 | 787| -------- | -------------------------------- | ---- | --------------------------------- | 788| callback | AsyncCallback<[Event](#event)[]> | 是 | 回调函数,返回的是Event对象数组。 | 789 790**示例**: 791 792```ts 793import { BusinessError } from '@ohos.base'; 794 795calendar.getEvents((err: BusinessError, data: calendarManager.Event[]) => { 796 if (err) { 797 console.error("Failed to get events"); 798 } else { 799 console.info("Succeeded in getting events"); 800 } 801}); 802``` 803 804### getEvents 805 806getEvents(eventFilter: EventFilter, eventKey: (keyof Event)[], callback: AsyncCallback\<Event[]>):void 807 808获取Calendar下符合查询条件的Event,使用callback异步回调。 809 810**系统能力**: SystemCapability.Applications.CalendarData 811 812**参数**: 813 814| 参数名 | 类型 | 必填 | 说明 | 815| ----------- | -------------------------------- | ---- | --------------------------------- | 816| eventFilter | [EventFilter](#eventfilter) | 是 | 查询条件。 | 817| eventKey | (keyof [Event](#event))[] | 是 | 查询字段。 | 818| callback | AsyncCallback<[Event](#event)[]> | 是 | 回调函数,返回的是Event对象数组。 | 819 820**示例**: 821 822```ts 823import { BusinessError } from '@ohos.base'; 824 825const filter = calendarManager.EventFilter.filterById([1, 2]); 826calendar.getEvents(filter, ['title', 'type', 'startTime', 'endTime'], (err: BusinessError, data: calendarManager.Event[]) => { 827 if (err) { 828 console.error("Failed to get events"); 829 } else { 830 console.info("Succeeded in getting events"); 831 } 832}); 833``` 834 835### getEvents 836 837getEvents(eventFilter?: EventFilter, eventKey?: (keyof Event)[]): Promise\<Event[]> 838 839获取Calendar下符合查询条件的Event,使用Promise异步回调。 840 841**系统能力**: SystemCapability.Applications.CalendarData 842 843**参数**: 844 845| 参数名 | 类型 | 必填 | 说明 | 846| ----------- | --------------------------- | ---- | ---------- | 847| eventFilter | [EventFilter](#eventfilter) | 否 | 查询条件。 | 848| eventKey | (keyof [Event](#event))[] | 否 | 查询字段。 | 849 850**返回值**: 851 852| 类型 | 说明 | 853| -------------------------- | ----------------------------------- | 854| Promise<[Event](#event)[]> | Promise对象,返回日程配置信息数组。 | 855 856**示例**: 857 858```ts 859import { BusinessError } from '@ohos.base'; 860 861const filter = calendarManager.EventFilter.filterByTitle('MyEvent'); 862calendar.getEvents(filter).then((data: calendarManager.Event[]) => { 863 console.info("Succeeded in getting events"); 864}).catch((err: BusinessError) => { 865 console.error("Failed to get events"); 866}); 867``` 868 869### getConfig 870 871getConfig(): CalendarConfig 872 873获取日历配置信息。 874 875**系统能力**: SystemCapability.Applications.CalendarData 876 877**返回值**: 878 879| 类型 | 说明 | 880| --------------------------------- | -------------- | 881| [CalendarConfig](#calendarconfig) | 日历配置信息。 | 882 883**示例**: 884 885```ts 886const config = calendar.getConfig(); 887console.info("get config success"); 888``` 889 890### setConfig 891 892setConfig(config: CalendarConfig, callback: AsyncCallback\<void>): void 893 894设置日历配置信息,使用callback异步回调。 895 896**系统能力**: SystemCapability.Applications.CalendarData 897 898**参数**: 899 900| 参数名 | 类型 | 必填 | 说明 | 901| -------- | --------------------------------- | ---- | -------------- | 902| config | [CalendarConfig](#calendarconfig) | 是 | 日历配置信息。 | 903| callback | AsyncCallback\<void> | 是 | 回调函数。 | 904 905**示例**: 906 907```ts 908import { BusinessError } from '@ohos.base'; 909 910const config: calendarManager.CalendarConfig = { 911 enableReminder: true 912}; 913calendar.setConfig(config, (err: BusinessError) => { 914 if (err) { 915 console.error("Failed to set config"); 916 } else { 917 console.info("Succeeded in setting config"); 918 } 919}); 920``` 921 922### setConfig 923 924setConfig(config: CalendarConfig): Promise\<void> 925 926设置日历配置信息,使用Promise异步回调。 927 928**系统能力**: SystemCapability.Applications.CalendarData 929 930**参数**: 931 932| 参数名 | 类型 | 必填 | 说明 | 933| ------ | --------------------------------- | ---- | -------------- | 934| config | [CalendarConfig](#calendarconfig) | 是 | 日历配置信息。 | 935 936**返回值**: 937 938| 类型 | 说明 | 939| -------------- | ------------------------- | 940| Promise\<void> | 无返回结果的Promise对象。 | 941 942**示例**: 943 944```ts 945import { BusinessError } from '@ohos.base'; 946 947const config: calendarManager.CalendarConfig = { 948 enableReminder: true 949}; 950calendar.setConfig(config).then(() => { 951 console.info("Succeeded in setting config"); 952}).catch((err: BusinessError) => { 953 console.error("Failed to set config"); 954}); 955``` 956 957### getAccount 958 959getAccount(): CalendarAccount 960 961获取日历账户信息。 962 963**系统能力**: SystemCapability.Applications.CalendarData 964 965**返回值**: 966 967| 类型 | 说明 | 968| ----------------------------------- | -------------- | 969| [CalendarAccount](#calendaraccount) | 日历帐户信息。 | 970 971**示例**: 972 973```ts 974const account = calendar.getAccount(); 975console.info("get account success"); 976``` 977 978## CalendarAccount 979 980日历帐户信息。 981 982**系统能力**:SystemCapability.Applications.CalendarData 983 984| 名称 | 类型 | 只读 | 必填 | 说明 | 985| ----------- | ----------------------------- | ---- | ---- | -------------------------------------- | 986| name | string | 是 | 是 | 帐户名称。 | 987| type | [CalendarType](#calendartype) | 否 | 是 | 帐户类型。 | 988| displayName | string | 否 | 否 | 帐户的显示名称。不填时,默认为空字符串。 | 989 990## CalendarConfig 991 992日历配置信息。 993 994**系统能力**:SystemCapability.Applications.CalendarData 995 996| 名称 | 类型 | 只读 | 必填 | 说明 | 997| -------------- | --------------------------------------------------- | ---- | ---- | ------------------------------------------------------------ | 998| enableReminder | boolean | 否 | 否 | 是否打开Calendar下所有Event提醒能力。当取值为true时,该Calendar下所有Event具备提醒能力;当取值为false时,不具备提醒能力,默认具备提醒能力。 | 999| color | [ResourceColor](../arkui-ts/ts-types.md#resourcecolor) | 否 | 否 | 设置Calendar颜色。不填时,默认值为'#0A59F7'。 | 1000 1001## Event 1002 1003日程对象,包含日程标题、开始时间、结束时间等信息。 1004 1005**系统能力**:SystemCapability.Applications.CalendarData 1006 1007| 名称 | 类型 | 只读 | 必填 | 说明 | 1008| -------------- | --------------------------------- | ---- | ---- | ------------------------------------------------------------ | 1009| id | number | 是 | 否 | 日程id。当调用[addEvent()](#addevent)、[addEvents()](#addevents)创建日程时,不填写此参数。 | 1010| type | [EventType](#eventtype) | 否 | 是 | 日程类型。 | 1011| title | string | 否 | 否 | 日程标题。不填时,默认为空字符串。 | 1012| location | [Location](#location) | 否 | 否 | 日程地点。不填时,默认为null。 | 1013| startTime | number | 否 | 是 | 日程开始时间。 | 1014| endTime | number | 否 | 是 | 日程结束时间。 | 1015| isAllDay | boolean | 否 | 否 | 是否为全天日程。当取值为true时,说明为全天日程;当取值为false时,说明不是全天日程,默认为非全天日程。 | 1016| attendee | [Attendee](#attendee)[] | 否 | 否 | 日程参与者。不填时,默认为null。 | 1017| timeZone | string | 否 | 否 | 日程时区。不填时,默认为当前所在时区,当需要创建与当前不一样的时区时,可填入对应的时区。可通过[getTimeZone()](js-apis-system-date-time.md#systemdatetimegettimezone)获取当前系统时区。 | 1018| reminderTime | number[] | 否 | 否 | 日程提醒时间。不填时,默认为不提醒。 | 1019| recurrenceRule | [RecurrenceRule](#recurrencerule) | 否 | 否 | 日程重复规则。不填时,默认为不重复。 | 1020| description | string | 否 | 否 | 日程描述。不填时,默认为空字符串。 | 1021| service | [EventService](#eventservice) | 否 | 否 | 日程服务。不填时,默认没有一键服务。 | 1022 1023## CalendarType 1024 1025帐户类型枚举。 1026 1027**系统能力**:SystemCapability.Applications.CalendarData 1028 1029| 名称 | 值 | 说明 | 1030| ---------- | ------------ | -------------------- | 1031| LOCAL | 'local' | 本地帐户。 | 1032| EMAIL | 'email' | 邮箱帐户。 | 1033| BIRTHDAY | 'birthday' | 生日帐户。 | 1034| CALDAV | 'caldav' | 支持CalDAV协议帐户。 | 1035| SUBSCRIBED | 'subscribed' | 订阅帐户。 | 1036 1037## Location 1038 1039日程地点。 1040 1041**系统能力**:SystemCapability.Applications.CalendarData 1042 1043| 名称 | 类型 | 只读 | 必填 | 说明 | 1044| --------- | ------ | ---- | ---- | ------------------------ | 1045| location | string | 否 | 否 | 地点位置。默认为空字符串。 | 1046| longitude | number | 否 | 否 | 地点经度。默认为0。 | 1047| latitude | number | 否 | 否 | 地点纬度。默认为0。 | 1048 1049## EventFilter 1050 1051日程过滤器,查询日程时进行筛选过滤,获取符合条件的日程。 1052 1053通过[filterById()](#filterbyid)、[filterByTime()](#filterbytime)、[filterByTitle()](#filterbytitle)任一方法获取日程过滤器,传入[getEvents()](#getevents)过滤。 1054 1055### filterById 1056 1057static filterById(ids: number[]): EventFilter 1058 1059根据日程id过滤日程。 1060 1061**系统能力**: SystemCapability.Applications.CalendarData 1062 1063**参数**: 1064 1065| 参数名 | 类型 | 必填 | 说明 | 1066| ------ | -------- | ---- | ------------ | 1067| ids | number[] | 是 | 日程id数组。 | 1068 1069**返回值**: 1070 1071| 类型 | 说明 | 1072| --------------------------- | -------------------- | 1073| [EventFilter](#eventfilter) | 返回日程过滤器对象。 | 1074 1075**示例**: 1076 1077```ts 1078import { BusinessError } from '@ohos.base'; 1079 1080const filter = calendarManager.EventFilter.filterById([1, 2]); 1081calendar.getEvents(filter).then((data: calendarManager.Event[]) => { 1082 console.info("Succeeded in filtering by id"); 1083}).catch((err: BusinessError) => { 1084 console.error("Failed to filter by id"); 1085}); 1086``` 1087 1088### filterByTime 1089 1090static filterByTime(start: number, end: number): EventFilter 1091 1092根据日程时间过滤日程。 1093 1094**系统能力**: SystemCapability.Applications.CalendarData 1095 1096**参数**: 1097 1098| 参数名 | 类型 | 必填 | 说明 | 1099| ------ | ------ | ---- | ---------- | 1100| start | number | 是 | 开始时间。 | 1101| end | number | 是 | 结束时间。 | 1102 1103**返回值**: 1104 1105| 类型 | 说明 | 1106| --------------------------- | -------------------- | 1107| [EventFilter](#eventfilter) | 返回日程过滤器对象。 | 1108 1109**示例**: 1110 1111```ts 1112import { BusinessError } from '@ohos.base'; 1113 1114const filter = calendarManager.EventFilter.filterByTime(1686931200000, 1687017600000); 1115calendar.getEvents(filter).then((data: calendarManager.Event[]) => { 1116 console.info("Succeeded in filtering by time"); 1117}).catch((err: BusinessError) => { 1118 console.error("Failed to filter by time"); 1119}); 1120``` 1121 1122### filterByTitle 1123 1124static filterByTitle(title: string): EventFilter 1125 1126根据日程标题过滤日程。 1127 1128**系统能力**: SystemCapability.Applications.CalendarData 1129 1130**参数**: 1131 1132| 参数名 | 类型 | 必填 | 说明 | 1133| ------ | ------ | ---- | ---------- | 1134| title | string | 是 | 日程标题。 | 1135 1136**返回值**: 1137 1138| 类型 | 说明 | 1139| --------------------------- | -------------------- | 1140| [EventFilter](#eventfilter) | 返回日程过滤器对象。 | 1141 1142**示例**: 1143 1144```ts 1145import { BusinessError } from '@ohos.base'; 1146 1147const filter = calendarManager.EventFilter.filterByTitle('MyEvent'); 1148calendar.getEvents(filter).then((data: calendarManager.Event[]) => { 1149 console.info("Succeeded in filtering by title"); 1150}).catch((err: BusinessError) => { 1151 console.error("Failed to filter by title"); 1152}); 1153``` 1154 1155## EventType 1156 1157日程类型枚举。 1158 1159**系统能力**:SystemCapability.Applications.CalendarData 1160 1161| 名称 | 值 | 说明 | 1162| --------- | ---- | -------------------- | 1163| NORMAL | 0 | 普通日程。 | 1164| IMPORTANT | 1 | 重要日程。支持倒计时。 | 1165 1166## RecurrenceRule 1167 1168日程重复规则。 1169 1170**系统能力**:SystemCapability.Applications.CalendarData 1171 1172| 名称 | 类型 | 只读 | 必填 | 说明 | 1173| ------------------- | ------------------------------------------- | ---- | ---- | ------------------------------- | 1174| recurrenceFrequency | [RecurrenceFrequency](#recurrencefrequency) | 否 | 是 | 日程重复规则类型。 | 1175| expire | number | 否 | 否 | 重复周期截止日。不填时,默认为0。 | 1176 1177## RecurrenceFrequency 1178 1179日程重复规则类型枚举。 1180 1181**系统能力**:SystemCapability.Applications.CalendarData 1182 1183| 名称 | 值 | 说明 | 1184| ------- | ---- | ---------- | 1185| YEARLY | 0 | 每年重复。 | 1186| MONTHLY | 1 | 每月重复。 | 1187| WEEKLY | 2 | 每周重复。 | 1188| DAILY | 3 | 每天重复。 | 1189 1190## Attendee 1191 1192日程参与者。 1193 1194**系统能力**:SystemCapability.Applications.CalendarData 1195 1196| 名称 | 类型 | 只读 | 必填 | 说明 | 1197| ----- | ------ | ---- | ---- | -------------- | 1198| name | string | 否 | 是 | 参与者的姓名。 | 1199| email | string | 否 | 是 | 参与者的邮箱。 | 1200 1201## EventService 1202 1203日程服务。 1204 1205**系统能力**:SystemCapability.Applications.CalendarData 1206 1207| 名称 | 类型 | 只读 | 必填 | 说明 | 1208| ----------- | --------------------------- | ---- | ---- | ------------------------------------- | 1209| type | [ServiceType](#servicetype) | 否 | 是 | 服务类型。 | 1210| uri | string | 否 | 是 | 服务的uri。可以跳转到三方应用相应界面。 | 1211| description | string | 否 | 否 | 服务辅助描述。不填时,默认为空字符串。 | 1212 1213## ServiceType 1214 1215日程服务类型枚举。 1216 1217**系统能力**:SystemCapability.Applications.CalendarData 1218 1219| 名称 | 值 | 说明 | 1220| --------------- | ---------------- | ------------ | 1221| MEETING | 'Meeting' | 一键入会。 | 1222| WATCHING | 'Watching' | 一键追剧。 | 1223| REPAYMENT | 'Repayment' | 一键还款。 | 1224| LIVE | 'Live' | 一键直播。 | 1225| SHOPPING | 'Shopping' | 一键购物。 | 1226| TRIP | 'Trip' | 一键查看。 | 1227| CLASS | 'Class' | 一键上课。 | 1228| SPORTS_EVENTS | 'SportsEvents' | 一键看赛事。 | 1229| SPORTS_EXERCISE | 'SportsExercise' | 一键运动。 |