From: http://blog.csdn.net/vrhero/archive/2008/01/13/2042481.aspx
/// <Summary>
/// Obtain the start and end dates of the specified week in a year. The start date follows ISO 8601, that is, Monday.
/// </Summary>
/// <Remarks> Write by vrhero </Remarks>
/// <Param name = "year"> Year (1 to 9999) </Param>
/// <Param name = "weeks"> Week (1 to 53) </Param>
/// <Param name = "weekrule"> Determine the rule for the first week </Param>
/// <Param name = "first"> When this method is returned, it contains the system. datetime value of the week's start date specified by year and weeks. If it fails, it is system. datetime. minvalue. If the year or weeks parameter exceeds the valid range, the operation fails. This parameter is passed without initialization. </Param>
/// <Param name = "last"> When this method is returned, it contains the system. datetime value of the end date of the week specified by year and weeks. If it fails, it is system. datetime. minvalue. If the year or weeks parameter exceeds the valid range, the operation fails. This parameter is passed without initialization. </Param>
/// <Returns> True is returned. Otherwise, false is returned. </Returns>
Public Static Bool Getdaysofweeks ( Int Year, Int Weeks, calendarweekrule weekrule, Out Datetime first, Out Datetime last)
{
// Initialize out parameters
First = Datetime. minvalue;
Last = Datetime. minvalue;
// Don't explain it...
If (Year < 1 | Year > 9999 )
Return False ;
// People from all over the world know this for up to 53 weeks a year...
If (Weeks < 1 | Weeks > 53 )
Return False ;
// Take the first day of the current year as the benchmark... why? It's easy...
Datetime firstcurr = New Datetime (year, 1 , 1 );
// Take the first day of a year for calculation...
Datetime firstnext = New Datetime (Year + 1 , 1 , 1 );
// Convert the day of the first day of the current year to a number... Sunday special processing... ISO 8601 standard...
Int Dayofweekfirst = ( Int ) Firstcurr. dayofweek;
If (Dayofweekfirst = 0 ) Dayofweekfirst = 7 ;
// The first day of the week without verification...
First = Firstcurr. adddays (Weeks - 1 ) * 7 - Dayofweekfirst + 1 );
// The first day of the week is the date of the previous year...
If (First. Year < Year)
{
Switch (Weekrule)
{
Case Calendarweekrule. firstday:
// Don't explain it...
First = Firstcurr;
Break ;
Case Calendarweekrule. firstfullweek:
// Extend by one week...
First = First. adddays ( 7 );
Break ;
Case Calendarweekrule. firstfourdayweek:
// The first day of the week is extended by one week after four days from the first day of the year...
If (Firstcurr. Subtract (first). Days > 3 )
{
First=First. adddays (7);
}
Break ;
Default :
Break ;
}
}
// Unverified weekend days...
Last = First. adddays ( 7 ). Addseconds ( - 1 );
// The weekend is the date of the next year...
If (Last. Year > Year)
{
Switch (Weekrule)
{
Case Calendarweekrule. firstday:
Last = Firstnext. addseconds ( - 1 );
Break ;
Case Calendarweekrule. firstfullweek:
// No need to handle
Break ;
Case Calendarweekrule. firstfourdayweek:
// One week ahead of the first day of the next year...
If (Firstnext. Subtract (first). Days < 4 )
{< br> first = first. adddays ( - 7 );
last = last. adddays ( - 7 );
}
Break ;
Default :
Break ;
}
}
Return True ;
}