How to get Start and End Dates in the .NET MAUI Calendar (SfCalendar)?
In the Syncfusion® .NET MAUI Calendar control, you can obtain the start and end dates of the selected range using the SelectionMode property and the SelectionChanged event of SfCalendar.
Step 1: Set the SelectionMode property of SfCalendar to Range to enable selecting a date range.
XAML
<calendar:SfCalendar x:Name="calendar"
SelectionMode="Range" />
Step 2: To display the start and end date of the selected range, use the DisplayAlert() method inside the SelectionChanged event handler.
XAML
<calendar:SfCalendar x:Name="calendar"
SelectionMode="Range">
<calendar:SfCalendar.Behaviors>
<local:CalendarBehavior />
</calendar:SfCalendar.Behaviors>
</calendar:SfCalendar>
C#
public class CalendarBehavior : Behavior<SfCalendar>
{
private SfCalendar sfCalendar;
private DateTime startDate;
private DateTime endDate;
protected override void OnAttachedTo(SfCalendar bindable)
{
base.OnAttachedTo(bindable);
this.sfCalendar = bindable;
this.sfCalendar.SelectionChanged += SfCalendar_SelectionChanged;
}
private void SfCalendar_SelectionChanged(object sender, CalendarSelectionChangedEventArgs e)
{
if (this.sfCalendar.SelectedDateRange != null)
{
startDate = (DateTime)this.sfCalendar.SelectedDateRange.StartDate;
if (this.sfCalendar.SelectedDateRange.EndDate != null)
{
endDate = (DateTime)this.sfCalendar.SelectedDateRange.EndDate;
}
else
{
endDate = (DateTime)this.sfCalendar.SelectedDateRange.StartDate;
}
App.Current.MainPage.DisplayAlert("StartDate:" + " " + startDate.ToString("dd/MM/yyyy"), "EndDate:" + " " + endDate.ToString("dd/MM/yyyy"), "OK");
}
}
protected override void OnDetachingFrom(SfCalendar bindable)
{
base.OnDetachingFrom(bindable);
if (this.sfCalendar != null)
{
this.sfCalendar.SelectionChanged -= SfCalendar_SelectionChanged;
}
this.sfCalendar = null;
}
}
Download the complete sample on GitHub
Output
Conclusion
I hope you enjoyed learning how to get the start and end date of the selected range in .NET MAUI Calendar (SfCalendar).
You can refer to our .NET MAUI Calendar’s feature tour page to know 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 comments below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!