Sometimes, I want to calculate the day of the day in ASP.NET. The following code can make this calculation. (There are many sample code on the Internet, but I have seen a lot of mistakes. And I have been reprinted to reprint, it is spread. I am here to pay and correct it. Ha ha.)
String CaculateWeekDay (int y, int m, int d) {if (m == 1) {m = 13; y ---;} if (m == 2) {m = 14; y -;} int week = (D 2 * m 3 * (m 1) / 5 y y / 4-y / 100 y / 400)% 7 1; String Weekstr = ""; Switch (Week) {Case 1: Weekstr = "Monday"; Break; Case 2: Weekstr = "Tuesday"; Break; Case 3: Weekstr = "Wednesday"; Break; Case 4: Weekstr = "Thursday"; Break; Case 5: Weekstr = "Friday" Break; Case 6: Weekstr = "Saturday"; Break; Case 7: Weekstr = "Sunday"; Break;}
return weekstr;} private void Button1_Click (object sender, System.EventArgs e) {int year = int.Parse (TextBox1.Text); int month = int.Parse (TextBox2.Text); int day = int.Parse (TextBox3. Text); label1.text = Caculateweekday (Year, Month, day);
The code is very simple, don't need too much explanation, I will understand it. If applied to reality, it is best to check TEXTBOX to prevent input illegal data; or change TextBox to DropDownList. Maybe someone will ask if there is a corresponding function in C #? Of course it has. Dayofweek is a function of returning to the day of the week. Since writing here, give an example code. as follows:
System.Datetime newdate = new datetime (2005, 1, 11); response.write (newdate.dayofwek);
Other instructions: Calculate the algorithm of the week, there is a Caile formula, and some formulas improved on the basis of the Kimlarson formula and the Caile formula.