- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Text.RegularExpressions;
- namespace ConsoleApplication7
- {
- class Program
- {
- public static int GetYear(string str)
- {
- List<int> list = new List<int>();
- MatchCollection matches = Regex.Matches(str, @"\d+");
- foreach (Match item in matches)
- {
- list.Add(Convert.ToInt32(item.Value));
- }
- return list[0];
- }
- public static int GetMonth(string str)
- {
- List<int> list = new List<int>();
- MatchCollection matches = Regex.Matches(str, @"\d+");
- foreach (Match item in matches)
- {
- list.Add(Convert.ToInt32(item.Value));
- }
- return list[1];
- }
- public static int GetDay(string str)
- {
- List<int> list = new List<int>();
- MatchCollection matches = Regex.Matches(str, @"\d+");
- foreach (Match item in matches)
- {
- list.Add(Convert.ToInt32(item.Value));
- }
- return list[2];
- }
- public static bool IsYear(string str)
- {
- int year = GetYear(str);
- if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0))
- {
- return true;
- }
- return false;
- }
- public static string GetWeek(int day)
- {
- Dictionary<int, string> dictionary = new Dictionary<int, string>();
- dictionary.Add(0, "星期一");
- dictionary.Add(1, "星期二");
- dictionary.Add(2, "星期三");
- dictionary.Add(3, "星期四");
- dictionary.Add(4, "星期五");
- dictionary.Add(5, "星期六");
- dictionary.Add(6, "星期日");
- if (dictionary.ContainsKey(day))
- {
- return dictionary[day];
- }
- return "未知的日期";
- }
- static void Main(string[] args)
- {
- string str = "2013-09-04";
- bool flag = IsYear(str);
- int days = GetDay(str);
- int month=GetMonth(str);
- for (int i = 1; i < month; i++)
- {
- switch (i)
- {
- case 1:
- case 3:
- case 5:
- case 7:
- case 8:
- case 10:
- case 12:
- days = days + 31;
- break;
- case 2:
- if (flag == true)
- {
- days = days + 29;
- }
- else
- {
- days = days + 28;
- }
- break;
- case 4:
- case 6:
- case 9:
- case 11:
- days = days + 30;
- break;
- default:
- break;
- }
- }
- string week = GetWeek(days % 7);
- Console.WriteLine("{0}是一年中的第{1}天,这一天是{2}",str,days,week);
- Console.ReadKey();
- }
- }
- }
复制代码 |