This example uses the Kim Larson calculation formula to calculate the day of the week based on the date:
First, let’s take a look at the definition of the Kim Larson calculation formula in Baidu Encyclopedia:
Kim Larson 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 difference in the formula from other formulas:
Consider January and February as the thirteenth and fourteenth months of the previous year. For example: if it is 2004-1-10, it will be converted into: 2003-13-10 to substitute the formula to calculate.
1. Client (called in ajax mode):
$.get('CaculateWeekDay', { y: 2016, m: 8, d: 9 }, function (result) { alert(result); })
2. Server:
/// <summary> /// Calculate the specific date 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 += 12; 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"; 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 = "Skies"; break; } return weekstr; }
The above is the example code for C# calculation of the day of the week based on dates introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support for my website!