The user enters a date to calculate the days of the week and the day ordinal of the year

Source: Internet
Author: User

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 ();


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.
<param name= "Y" > Year </param>
<param name= "M" > Month </param>
<param name= "D" > Day </param>
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;

Enter the date of the year and return to the day ordinal of the first.
<param name= "Y" > Year </param>
<param name= "M" > Month </param>
<param name= "D" > Day </param>
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;
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)
return sum;



You are welcome to make comments, there is a better way to communicate with everyone. I qq:214284213

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: 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.