using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace FileWatcher { public partial class DateWeek : Form { public DateWeek() { InitializeComponent(); } private void DateWeek_Load(object sender, EventArgs e) { } private void button1_Click(object sender, EventArgs e) { int year, year1, year2, month1, month2, day1, day2, week, sum = 0; int[] days = new int[] { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; int i; if (textBox1.Text.Length != 4) { MessageBox.Show("请输入格式如2040,年2位,周数2位"); return; } Console.Write("年:"); year = int.Parse(textBox1.Text.Substring(0, 2)); Console.Write("周次:"); week = int.Parse(textBox1.Text.Substring(2, 2)); if (year < 0) { Console.WriteLine("年份错误,必须大于等于0"); return; } if (week < 1 || week > 54) { Console.WriteLine("周次错误,必须位于 1 .. 53 之间"); return; } sum += 7 * (week - 1); //修正总天数 switch (DateTime.Parse(year + "-1-1").DayOfWeek) { case DayOfWeek.Tuesday: { sum -= 1; break; } case DayOfWeek.Wednesday: { sum -= 2; break; } case DayOfWeek.Thursday: { sum -= 3; break; } case DayOfWeek.Friday: { sum -= 4; break; } case DayOfWeek.Saturday: { sum -= 5; break; } case DayOfWeek.Sunday: { sum -= 6; break; } default: break; } if ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0)) { days[1] = 29; } for (i = 0; i < days.Length; i++) { if (sum >= days[i]) { sum -= days[i]; } else { break; } } year1 = year; month1 = i + 1; day1 = sum + 1; //修改开始日期 if (day1 < 1) { day1 += days[11]; month1 = 12; year1--; } year2 = year1; month2 = month1; day2 = day1 + 6; if (day2 > days[month1 - 1]) { day2 -= days[month1 - 1]; month2++; if (month2 > 12) { month2 = 1; year2++; } } label1.Text = year + "年第" + week + "周 : " + year1 + "年" + month1 + "月" + day1 + "日 ~ " + year2 + "年" + month2 + "月" + day2 + "日"; } } }