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 obtain the month and year of current view using the ViewChanged event of the SfCalendar.

 

Step 1: Add the SfCalendar and Label controls to your layout to display the month and year, 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: Use the DisplayDate property of the SfCalendar to get the current visible month and year when the calendar loads. Update the labels with the current month and year using the ViewChanged event.

 

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;
    }
}

 

Download the complete sample on [GitHub]

 

Output

 

Get month and year in the Syncfusion .NET MAUI Calendar

 

Conclusion

 

I hope you enjoyed learning 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. 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