Algorithm used to calculate the day of a week

Source: Internet
Author: User
W = (D + 2 * m + 3 * (m + 1)/5 + Y/4-y/100 + Y/400) mod 7
In the formula, D indicates the date in the date, M indicates the number of months, and y indicates the number of years.

Note: In this formula, and are regarded as the 13th and 14th months of the previous year. For example, if-1-10 is used, the formula is converted to 2003-13-10 for calculation.

Below is C #'sCodeImplementation

Code
///   <Summary>
/// Calculate the week of the input date
///   </Summary>
///   <Param name = "year"> Year, yyyy format: 2009 </Param>
///   <Param name = "month"> Month: an integer between 1 and 12 </Param>
///   <Param name = "day"> Date: an integer between 1 and 31 </Param>
///   <Returns> The number of weeks. One to seven represents Monday and seven represents Sunday. </Returns>
Int Week ( Int Year, Int Month, Int Day ){

If (Month =   1   | Month =   2 ){
Month + =   12 ;
Year -=   1 ;
}

Int Week = (Day +   2   * Month +   3   * (Month +   1 ) /   5   + Year + Year /   4   - Year /   100   + Year /   400 ) %   7 ;
Return (Week +   1 );
}

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.