joomla visitors
Mithila Software Blog | Getting the number of days in a selected month using Calender control

Getting the number of days in a selected month using Calender control

December 22, 2014 Posted by Himanshu

Select month and calculate all days of this month

To get the solution to this problem we will use

Drag and drop one calender control and name it Calender1 and one Label control name it Label1 to display the result from the calender control.

We attach and event handler in Page_Load event named  

Calendar1.VisibleMonthChanged += Calendar1_VisibleMonthChanged;

This is because we need to displays days in a month as soon the calender control's forward or backward button is pressed.

The days in a month processing will be handled in this funtion

    void Calendar1_VisibleMonthChanged(object sender, MonthChangedEventArgs e)

Now lets get on to the codebehind that we need to understand

 

 

 

Insert code here...
protected void Page_Load(object sender, EventArgs e) { Calendar1.VisibleMonthChanged += Calendar1_VisibleMonthChanged; } void Calendar1_VisibleMonthChanged(object sender, MonthChangedEventArgs e) { int months = Calendar1.VisibleDate.Date.Month; int days = calaculatedaysinamonth(months); Label1.Text = string.Format("The selected month has {0} days",days.ToString()); } public int calaculatedaysinamonth(int m) { int tdays = 0; if (m == 1) { tdays = 31; return tdays; } else if (m == 2) { int curryear = Calendar1.VisibleDate.Year; bool varResult = DateTime.IsLeapYear(curryear); if (varResult.Equals(true)) { tdays = 29; return tdays; } else { tdays = 28; return tdays; } return tdays; } else if (m == 3) { tdays = 31; return tdays; } else if (m == 4) { tdays = 30; return tdays; } else if (m == 5) { tdays = 31; return tdays; } else if (m == 6) { tdays = 30; return tdays; } else if (m == 7) { tdays = 31; return tdays; } else if (m == 8) { tdays = 31; return tdays; } else if (m == 9) { tdays = 30; return tdays; } else if (m == 10) { tdays = 31; return tdays; } else if (m == 11) { tdays = 30; return tdays; } else if (m == 12) { tdays = 31; return tdays; } return tdays; }
blog comments powered by Disqus
Address ::
Mithila Software Solutions,
Green House , 3 GM Road ,
Near Radio Station , Darbhanga
PIN:: 846004
Phone :: # 099-397-49350