/// 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 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>
/// 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)