C # To calculate the day of the week based on the date of the instance Code _c# tutorial

Source: Internet
Author: User

This example uses the Kimlarsson formula to calculate the day of the week based on the date:

First look at Baidu Encyclopedia of Kimlarsson formula definition:

  Kimlarsson Calculation formula

W= (d+2*m+3* (m+1)/5+y+y/4-y/100+y/400) MoD 7

In the formula D represents the number of days in the date, m represents the number of months, and Y represents the number of years.

  Note: There is a different formula in the formula:

Consider January and February as the January or March and January or April of the previous year, for example: if it is 2004-1-10, convert it to: 2003-13-10 to substitute the formula.

1, the client (using Ajax method calls):

$.get (' Caculateweekday ', {y:2016, M:8, D:9}, function (Result) {
alert (result);
})

2, service side:

<summary>
///Calculate a specific date is the day of the week
///</summary>
///<param name= "Y" > Year </param>
///<param name= "M" > Month </param>
///<param name= "D" > Day </param>
///<returns ></returns> public
string caculateweekday (int y, int m, int d) 
{
if (m = = 1 | | m = = 2) 
{
m + +;
y--;
}
Int week = (d + 2 * m + 3 * (M + 1)/5 + y + Y/4-y/100 + y/400)% 7;
String weekstr = "";
Switch (week)
{case
0:weekstr = "Monday";
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 = "Week Seven"; break;
return weekstr;
}

The above is a small set up to introduce C # based on the date of the day to calculate the example code of the week, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.