Search code examples
c#monthcalendar

get the year value in MonthCalendar using C#


I have a MonthCalendar in my Form. what i want to do is get the year value when I pick a date in the MonthCalendar.

example: when i pick 12/01/2021. i want to get the year value like "21" in 2021. below is my sample code:

private void calendar_DateChanged(object sender, DateRangeEventArgs e)
{
    string datepick = calendar.SelectionRange.Start.ToShortDateString();
}

thanks in advance. I'm using c# in virtual studio btw.


Solution

  • Try this:

    var Out = monthcalender1.SelectionRange
                            .Start
                            .ToString("yyyy");
    

    Your result will be "2021".

    Or if you want only "21" then try this:

    var Out = monthcalender1.SelectionRange
                            .Start
                            .ToString("yy");