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

Source: Internet
Author: User

/// The number of days 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>
/// Obtain the start and end days of a week in a year.
/// Year nyear
/// Week count nnumweek
/// Start every week out of dtweekstart
/// End of week 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>
/// Calculate the number of 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); // obtain the number of weeks on the first day of the year.
If (k = 1)
{
Int countweek = countday/7 + 1;
Return countweek;

}
Else
{
Int countweek = countday/7 + 2;
Return countweek;
}

}

/** // <Summary>
/// Calculate the week number of the year for the current date.
/// </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;

}

}

(Note: From http://www.cnblogs.com/yuanermen/archive/2008/07/14/1242853.html)

Related Keywords:
Related Article

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.