C # Calculate the week by date,

Source: Internet
Author: User

C # Calculate the week by date,

In this example, kemlerson formula is used to calculate the day of the week Based on the date:

First, let's take a look at the definition of the kemlerson formula in Baidu Encyclopedia:
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.

1. Client (called Using ajax ):

$. Get ('caculateweekday', {y: 2016, m: 8, d: 9}, function (result ){
Alert (result );
})

2. Server:

1 /// <summary> 2 /// calculate the day of the week for a specific date. 3 /// </summary> 4 /// <param name = "y"> year </param> 5 // <param name = "m"> month </param> 6 // <param name = "d"> day </param> 7 // /<returns> </returns> 8 public string CaculateWeekDay (int y, int m, int d) 9 {10 if (m = 1 | m = 2) 11 {12 m + = 12; 13 y --; 14} 15 int week = (d + 2 * m + 3 * (m + 1)/5 + y/4-y/100 + y/400) % 7; 16 string weekstr = ""; 17 switch (week) 18 {19 case 0: weekstr = "Monday"; break; 20 case 1: weekstr = "Tuesday"; break; 21 case 2: weekstr = "Wednesday"; break; 22 case 3: weekstr = "Thursday"; break; 23 case 4: weekstr = "Friday"; break; 24 case 5: weekstr = "Saturday"; break; 25 case 6: weekstr = "Saturday"; break; 26} 27 return weekstr; 28}

 

Related Article

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.