C # Calculation of the date and month days and the number of weeks in a year and the time period obtained by a certain week of the year

Source: Internet
Author: User

How many days are there in the current month
</summary>
<param name= "Y" ></param>
<param name= "M" ></param>
<returns></returns>
public static int Howmonthday (int y, int m)
{
int mnext;
int ynext;
if (M < 12)
{
Mnext = m + 1;
Ynext = y;
}
Else
{
Mnext = 1;
Ynext = y + 1;
}
DateTime dt1 = System.Convert.ToDateTime (y + "-" + M + "-1");
DateTime DT2 = System.Convert.ToDateTime (Ynext + "-" + Mnext + "-1");
TimeSpan diff = dt2-dt1;
Return diff. Days;
}

/**////<summary>
Get the start date and deadline of a week in a year
Year Nyear
Week number Nnumweek
Week start out Dtweekstart
Week End out Dtweekeend
</summary>
<param name= "Nyear" ></param>
<param name= "Nnumweek" ></param>
<param name= "Dtweekstart" ></param>
<param name= "Dtweekeend" ></param>
public static void Getweek (int nyear, int. Nnumweek, Out datetime dtweekstart, out datetime dtweekeend)
{
DateTime dt = new DateTime (Nyear, 1, 1);
DT = dt + new TimeSpan ((nNumWeek-1) * 7, 0, 0, 0);
Dtweekstart = dt. AddDays (-(int) dt. DayOfWeek + (int) dayofweek.monday);
dtweekeend = dt. AddDays ((int) dayofweek.saturday-(int) dt. DayOfWeek + 1);
}

       /**////<summary>
        ///How many weeks in a year
       //return int
        //</summary>
       //<param name= "Stryear" ></ Param>
       //<returns>int</returns>
         public static int getyearweekcount (int stryear)
         {
            string returnstr = "";

            System.DateTime fDt = DateTime.Parse ( Stryear.tostring () + "01-01");
            int k = Convert.ToInt32 (Fdt.dayofweek);// The first day of the year is the   of the week;
            if (k = = 1)
             {
                 int countday = fdt.addyears (1). AddDays (-1). DayOfYear;
                int countweek = COUNTDAY/7 + 1;
                return countweek;

}
Else
{
int countday = Fdt.addyears (1). AddDays (-1). DayOfYear;
int Countweek = COUNTDAY/7 + 2;
return countweek;
}

}

/**////<summary>
Ask the current date is the first week of the year
</summary>
<param name= "Date" ></param>
<returns></returns>
public static int WeekOfYear (DateTime curday)
{
int firstdayofweek = Convert.ToInt32 (Convert.todatetime (curDay.Year.ToString () + "-" + "1-1"). DayOfWeek);

int days = Curday.dayofyear;
int daysoutoneweek = days-(7-firstdayofweek);

            if (daysoutoneweek <= 0)
             {
                 return 1;
           }
            Else
             {
                 int weeks = DAYSOUTONEWEEK/7;
                if ( Daysoutoneweek% 7! = 0)
                     weeks++;

return weeks + 1;

}

}

C # Calculation of the date and month days and the number of weeks in a year and the time period obtained by a certain week of the year

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.