In Ionic Framework, the ion-datetime component does not have a built-in event specifically for the month change. However, you can achieve the desired behavior by using a combination of the ionChange event and a custom function to detect the month change.

Here's how you can add an event listener for the month change event in an ion-datetime component:

  1. Install Ionic: Ensure you have Ionic installed in your project. If you haven't already installed it, you can do so using the following command:

    npm install @ionic/angular --save
  2. Add ion-datetime Component: Add the ion-datetime component to your template with the displayFormat set to 'MM/YYYY' (month and year format) and [(ngModel)] to bind its value to a variable:

    <!-- Your template.html --> <ion-datetime displayFormat="MM/YYYY" [(ngModel)]="selectedDate" (ionChange)="onDateChange($event)" ></ion-datetime>
  3. Add Event Listener Function: In your component's TypeScript code, create a function (onDateChange in this example) to handle the ionChange event. Inside this function, you can check if the month has changed compared to the previous value.

    // Your component.ts import { Component } from '@angular/core'; @Component({ selector: 'app-your-component', templateUrl: './', styleUrls: ['./'], }) export class YourComponentPage { selectedDate: string; onDateChange(event: CustomEvent) { const newDate = new Date(event.detail.value); const previousDate = new Date(this.selectedDate); if (newDate.getMonth() !== previousDate.getMonth()) { console.log('Month has changed:', newDate.getMonth()); // Perform actions when the month changes } this.selectedDate = event.detail.value; } }

    In the onDateChange function, we compare the month of the newly selected date with the previous selected date's month. If they are different, it means the month has changed, and you can perform the desired actions inside the if block.

With this setup, the onDateChange function will be triggered whenever the user selects a new month in the ion-datetime component, allowing you to respond to the month change event.

Have questions or queries?
Get in Touch