1, write the program, the user presses "YYYY-MM-DD" format to enter a date, the program after the output of the day is the days of the week, and calculated is the first day of the year.
The following code is attached:
Calculated week using the: Kimlarsson calculation formula
Using System;
Namespace Test10
{
Class Program
{
10, write the program, the program started after the user can press "YYYY-MM-DD" format entered a date, the program calculates the day of the week, and calculated is the first day of the year.
static void Main (string[] args)
{
Prompts the user to enter a date type data
Console.WriteLine ("Please enter a date in the format of \ yyyy-mm-dd\");
String str = Console.ReadLine ();
Test with
String str = "2014-02-10";
Converts the string entered into a date type
DateTime dt = convert.todatetime (str);
Defines a string type that holds data for the day of the week
String weeks = Cacuweek (dt. Year, dt. Month, dt. Day);
Defines an integer variable that holds the number of days of data in a year
int days = cacuday (dt. Year, dt. Month, dt. Day);
Console.WriteLine ("{0} day is {1}", is {3} days in {2} years), STR, weeks, dt. year, days);
Console.readkey ();
}
<summary>
Enter the day of the month and return to the week
The algorithm is as follows:
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.
</summary>
<param name= "Y" > Year </param>
<param name= "M" > Month </param>
<param name= "D" > Day </param>
<returns></returns>
static string Cacuweek (int y, int m, int d)
{//y=2014 m=14 d=9
if (m = = 1) m = 13;
if (m = = 2) m = 14;
Int week = (d + 2 * m + 3 * (M + 1)/5 + y + Y/4-y/100 + y/400)% 7;
String weekstr = "";
Converts a number to a week's string
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 0:weekstr = "Sunday"; Break
}
return weekstr;
}
<summary>
Enter the date of the year and return to the day ordinal of the first.
</summary>
<param name= "Y" > Year </param>
<param name= "M" > Month </param>
<param name= "D" > Day </param>
<returns></returns>
static int cacuday (int y, int m, int d)
{
int sum = 0, leap = 0;
Determine the month, count the days of the month
Switch (m)
{
Case 1:sum = 0; Break
Case 2:sum = 31; Break
Case 3:sum = 59; Break
Case 4:sum = 90; Break
Case 5:sum = 120; Break
Case 6:sum = 151; Break
Case 7:sum = 181; Break
case 8:sum = 212; Break
Case 9:sum = 243; Break
Case 10:sum = 273; Break
Case 11:sum = 304; Break
Case 12:sum = 334; Break
}
Plus the number of days in a day
sum = sum + D;
Judge whether it is a leap year
If (y% 400 = 0 | | (y% 4 = 0 && y% 100!= 0))
{
leap = 1;
}
Else
{
leap = 0;
}
If it is a leap year and the month is greater than 2, total days plus 1 days
if (leap = = 1 && m > 2)
{
sum++;
}
return sum;
}
}
}
You are welcome to make comments, there is a better way to communicate with everyone. I qq:214284213