C # Gets the first and last day of the month, based on the year, and calculates the working day

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.