stringstr ="March 2015";intFirstIndex = str. IndexOf ("years",0);intSecondindex = str. IndexOf ("Month", FirstIndex +1);stringmonth = str. Substring (FirstIndex +1, Secondindex-firstindex-1);stringYear = str. Substring (0,4);D atetime DT=DateTime.Now;string[] time = dt. ToString (). Split (New Char[]{'/'});stringDate = year +"/"+ Month +"/"+ time[2];D atetime First= Convert.todatetime (date). AddDays (1-convert.todatetime (date). Day);D Atetime Last=convert.todatetime (date). AddDays (1-Convert.todatetime (date). Day). AddMonths (1). AddDays (-1);//The first day of the monthLabel1.Text =First . ToString ();//The last day of the monthLabel2.Text =Last . ToString ();//Businessdays of the month (except on the weekend)Label3.text = Getint (First, last). ToString ();
Getint Method:
Public intgetint (DateTime firstday, datetime lastday) {firstday=firstday.date; Lastday=lastday.date; if(FirstDay >lastday)Throw NewArgumentException ("Incorrect last day"+lastday); TimeSpan span= Lastday-FirstDay; intBusinessdays = span. Days +1; intFullweekcount = businessdays/7; //Find out if there is weekends during the time exceedng the full weeks if(Businessdays > Fullweekcount *7) { //We ' here ' find out if there is a 1-day or 2-days weekend//In the time interval remaining after subtracting the complete weeks intFirstDayOfWeek = Firstday.dayofweek = = DayOfWeek.Sunday?7: (int) Firstday.dayofweek; intLastdayofweek = Lastday.dayofweek = = DayOfWeek.Sunday?7: (int) Lastday.dayofweek; if(Lastdayofweek <firstdayofweek) Lastdayofweek+=7; if(FirstDayOfWeek <=6) { if(Lastdayofweek >=7)//Both Saturday and Sunday is in the remaining time intervalBusinessdays-=2; Else if(Lastdayofweek >=6)//Only Saturday are in the remaining time intervalBusinessdays-=1; } Else if(FirstDayOfWeek <=7&& Lastdayofweek >=7)//Only Sunday are in the remaining time intervalBusinessdays-=1; } //subtract the weekends during the full weeks in the intervalBusinessdays-= Fullweekcount +Fullweekcount; returnbusinessdays;}
C # Gets the first and last day of the month, based on the year, and calculates the working day