Articles in this section
Category / Section

How to Get the Month and Year in the .NET MAUI Calendar?

2 mins read

In the Syncfusion .NET MAUI Calendar control, you can get the month and year of month view using the ViewChanged event of the SfCalendar.

 

Step 1: Add the SfCalendar control and the Label control to your layout, as shown in the following code sample.

 

XAML

<ContentPage.Behaviors>
    <local:CalendarBehavior />
</ContentPage.Behaviors>
<ContentPage.Content>
    <Grid RowDefinitions="0.1*, 0.9*">
        <StackLayout>
            <Label x:Name="monthLabel"
                   HorizontalOptions="Center"
                   VerticalOptions="Center"
                   FontSize="20" />
            <Label x:Name="yearLabel"
                   HorizontalOptions="Center"
                   VerticalOptions="Center"
                   FontSize="20" />
        </StackLayout>
        <calendar:SfCalendar Grid.Row="1"
                             x:Name="calendar" />
    </Grid>
</ContentPage.Content>

 

Step 2: To display the month and year, use the DisplayDate property of the SfCalendar to get the current visible month and year when the calendar loads and set the month and year to the appropriate labels on your user interface. Use the same DisplayDate property inside the ViewChanged event to update the labels with the current month and year when the calendar view changes. The following code shows how to implement this step.

 

C#

public class CalendarBehavior : Behavior<ContentPage>
{
    private SfCalendar calendar;
    private Label monthLabel, yearLabel;
 
    protected override void OnAttachedTo(ContentPage bindable)
    {
        base.OnAttachedTo(bindable);
        this.calendar = bindable.FindByName<SfCalendar>("calendar");
        this.calendar.ViewChanged += Calendar_ViewChanged;
        this.monthLabel = bindable.FindByName<Label>("monthLabel");
        this.monthLabel.Text = "Month: " + this.calendar.DisplayDate.ToString("MMMM");
        this.yearLabel = bindable.FindByName<Label>("yearLabel");
        this.yearLabel.Text = "Year: " + this.calendar.DisplayDate.ToString("yyyy");
    }
 
    private void Calendar_ViewChanged(object sender, CalendarViewChangedEventArgs e)
    {
        this.monthLabel.Text = "Month: " + this.calendar.DisplayDate.ToString("MMMM");
        this.yearLabel.Text = "Year: " + this.calendar.DisplayDate.ToString("yyyy");
    }
 
    protected override void OnDetachingFrom(ContentPage bindable)
    {
        base.OnDetachingFrom(bindable);
        if (this.calendar != null)
        {
            this.calendar.ViewChanged -= Calendar_ViewChanged;
        }
 
        this.calendar = null;
        this.monthLabel = this.yearLabel = null;
    }
}

 

View sample on GitHub

 

Output

 

Get month and year in the Syncfusion .NET MAUI Calendar

 

Conclusion

 

Hope you enjoyed learning about how to get the month and year of the month view in the .NET MAUI Calendar.

You can refer to our .NET MAUI Calendar’s feature tour page to learn more about its other groundbreaking feature representations. You can explore our .NET MAUI Calendar documentation to understand how to present and manipulate data.

For current customers, you can check out our .NET MAUI components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our .NET MAUI Calendar and other .NET MAUI components.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied