C # determine the year, month, and day of the week based on the week of the year

Source: Internet
Author: User

// Determine the date based on the number of weeks
Private datetime getweekofdate (INT year, int I)
{
// Int month = 1;
Datetime yeard = new datetime (year, 1, 1 );
Int week = convert. toint32 (yeard. dayofweek );
If (week = 0)
{
Week = 7;
}

Int outday = I * 7-(week-1); // throw the excess days to the day of the week
Int sum = outday; // the number of days that have elapsed in the week. Sunday is the last day.
Int day = 6; // fixed borrow

Try
{
If (sum <7) // week 1
{
Yeard = new datetime (year-1, 12, 31-(day-sum ));
}
Else
{
For (INT x = 1; x <13; X ++)
{
Int nextmonth = system. datetime. daysinmonth (year, x + 1); // The next month
Int daysinjuly = system. datetime. daysinmonth (year, x); // number of days in the current month

If (Sum-daysinjuly) <0)
{
If (sum <= 6)
{
Int lastmonth = system. datetime. daysinmonth (year, X-1); // previous month
Yeard = new datetime (year, X-1, lastmonth-(day-sum ));
}
Else
If (sum <daysinjuly)
{
Yeard = new datetime (year, X, Sum-day );
}
Break;
}
Else
If (Sum-daysinjuly) <0 & (Sum-daysinjuly-nextmonth) <Day)
{
Day = sum-daysinjuly;
Yeard = new datetime (year, X, daysinjuly-day );
Break;
}
Sum = sum-daysinjuly;
}
}
}
Catch (exception ex) {MessageBox. Show ("data error! ");}
Return yeard;
}

 

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/fulminati/archive/2009/03/13/3987422.aspx

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.