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

Source: Internet
Author: User

AlgorithmAs follows:
Kemlerson Formula
W = (D + 2 * m + 3 * (m + 1)/5 + Y/4-y/100 + Y/400) mod 7

In the formula, D indicates the number of days in the date, M indicates the number of months, and y indicates the number of years.

Note: There is a difference between the formula and other formulas:

The year and month are regarded as the 13 and 14 months of the previous year. For example, if the year-1-10 is used, the formula is converted to 2003-13-10.

CodeAs follows:

// Y-year, M-month, D-Date
String Caculateweekday ( Int Y, Int M, Int D)
{
If (M = 1 ) M = 13 ;
If (M = 2 ) M = 14 ;
Int Week = (D + 2 * M + 3 * (M + 1 ) / 5 + Y + Y / 4 - Y / 100 + Y / 400 ) % 7 ;
String Weekstr = "" ;
Switch (Week)
{
  Case   7 : Weekstr =   " Monday " ; Break ;
  Case   1 : Weekstr =   " Tuesday " ; Break ;
  Case   2 : Weekstr =   " Wednesday " ; Break ;
  Case   3 : Weekstr =   " Thursday " ; Break ;
  Case   4 : Weekstr =   " Friday " ; Break ;
  Case   5 : Weekstr =   " Saturday " ; Break ;
  Case   6 : Weekstr =   " Sunday " ; Break ;
}
Return Weekstr;
}

 

Call method:
Label2.text = caculateweekday (maid, 9 );

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.