DateWeek.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. namespace FileWatcher
  10. {
  11. public partial class DateWeek : Form
  12. {
  13. public DateWeek()
  14. {
  15. InitializeComponent();
  16. }
  17. private void DateWeek_Load(object sender, EventArgs e)
  18. {
  19. }
  20. private void button1_Click(object sender, EventArgs e)
  21. {
  22. int year, year1, year2, month1, month2, day1, day2, week, sum = 0;
  23. int[] days = new int[] { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  24. int i;
  25. if (textBox1.Text.Length != 4)
  26. {
  27. MessageBox.Show("请输入格式如2040,年2位,周数2位");
  28. return;
  29. }
  30. Console.Write("年:");
  31. year = int.Parse(textBox1.Text.Substring(0, 2));
  32. Console.Write("周次:");
  33. week = int.Parse(textBox1.Text.Substring(2, 2));
  34. if (year < 0)
  35. {
  36. Console.WriteLine("年份错误,必须大于等于0");
  37. return;
  38. }
  39. if (week < 1 || week > 54)
  40. {
  41. Console.WriteLine("周次错误,必须位于 1 .. 53 之间");
  42. return;
  43. }
  44. sum += 7 * (week - 1);
  45. //修正总天数
  46. switch (DateTime.Parse(year + "-1-1").DayOfWeek)
  47. {
  48. case DayOfWeek.Tuesday:
  49. {
  50. sum -= 1;
  51. break;
  52. }
  53. case DayOfWeek.Wednesday:
  54. {
  55. sum -= 2;
  56. break;
  57. }
  58. case DayOfWeek.Thursday:
  59. {
  60. sum -= 3;
  61. break;
  62. }
  63. case DayOfWeek.Friday:
  64. {
  65. sum -= 4;
  66. break;
  67. }
  68. case DayOfWeek.Saturday:
  69. {
  70. sum -= 5;
  71. break;
  72. }
  73. case DayOfWeek.Sunday:
  74. {
  75. sum -= 6;
  76. break;
  77. }
  78. default:
  79. break;
  80. }
  81. if ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0))
  82. {
  83. days[1] = 29;
  84. }
  85. for (i = 0; i < days.Length; i++)
  86. {
  87. if (sum >= days[i])
  88. {
  89. sum -= days[i];
  90. }
  91. else
  92. {
  93. break;
  94. }
  95. }
  96. year1 = year;
  97. month1 = i + 1;
  98. day1 = sum + 1;
  99. //修改开始日期
  100. if (day1 < 1)
  101. {
  102. day1 += days[11];
  103. month1 = 12;
  104. year1--;
  105. }
  106. year2 = year1;
  107. month2 = month1;
  108. day2 = day1 + 6;
  109. if (day2 > days[month1 - 1])
  110. {
  111. day2 -= days[month1 - 1];
  112. month2++;
  113. if (month2 > 12)
  114. {
  115. month2 = 1;
  116. year2++;
  117. }
  118. }
  119. label1.Text = year + "年第" + week + "周 : " + year1 + "年" + month1 + "月" + day1 + "日 ~ " + year2 + "年" + month2 + "月" + day2 + "日";
  120. }
  121. }
  122. }