日期和字符串一样,都不能改变自身的值
1 System.DateTime d = System.DateTime.Now;
2 System.Console.WriteLine(d.AddDays(1));
3 System.Console.WriteLine(d.AddHours(1));
4 System.Console.WriteLine(d.AddMilliseconds(1));
5 System.Console.WriteLine(d.AddMinutes(1));
6 System.Console.WriteLine(d.AddMonths(1));
7 System.Console.WriteLine(d.AddYears(1));
8 System.Console.WriteLine(d);//d没有改变值
9 d = d.AddYears(1);//需要重新赋值
2 System.Console.WriteLine(d.AddDays(1));
3 System.Console.WriteLine(d.AddHours(1));
4 System.Console.WriteLine(d.AddMilliseconds(1));
5 System.Console.WriteLine(d.AddMinutes(1));
6 System.Console.WriteLine(d.AddMonths(1));
7 System.Console.WriteLine(d.AddYears(1));
8 System.Console.WriteLine(d);//d没有改变值
9 d = d.AddYears(1);//需要重新赋值
一些关于日期的算法要牢记
1 System.Console.WriteLine(d.AddDays(-(d.Day-1)));//本月第一天
2 System.Console.WriteLine(d.AddDays(-d.Day));//上月最后一天
3 System.Console.WriteLine(d.AddDays(-(int)d.DayOfWeek));//本周第一天
2 System.Console.WriteLine(d.AddDays(-d.Day));//上月最后一天
3 System.Console.WriteLine(d.AddDays(-(int)d.DayOfWeek));//本周第一天
ok,我们来看一个例子:
1 System.DateTime dt = new System.DateTime(1949, 10, 1);
2 System.Console.WriteLine(dt.ToLongDateString());
3 System.DateTime d = System.DateTime.Now;
4 int toDay = d.Day;
5 int thisMonth = d.Month;
6 System.Console.WriteLine("当前日期:{0}", d.Date);
7 d = d.AddDays(-(toDay - 1));
8 System.Console.WriteLine("该月第一天:{0}", d.Date);
9
10 do
11 {
12 string toDayFlag = "";
13 if (d.Day == toDay)
14 {
15 toDayFlag = "*";
16 }
17 System.Console.WriteLine("{0}:{1}{2}", d.Day, d.DayOfWeek, toDayFlag);
18 d = d.AddDays(1);
19 }
20 while (d.Month == thisMonth);
2 System.Console.WriteLine(dt.ToLongDateString());
3 System.DateTime d = System.DateTime.Now;
4 int toDay = d.Day;
5 int thisMonth = d.Month;
6 System.Console.WriteLine("当前日期:{0}", d.Date);
7 d = d.AddDays(-(toDay - 1));
8 System.Console.WriteLine("该月第一天:{0}", d.Date);
9
10 do
11 {
12 string toDayFlag = "";
13 if (d.Day == toDay)
14 {
15 toDayFlag = "*";
16 }
17 System.Console.WriteLine("{0}:{1}{2}", d.Day, d.DayOfWeek, toDayFlag);
18 d = d.AddDays(1);
19 }
20 while (d.Month == thisMonth);
上面例子输出本月的所有日期和星期,并在当日标注*
那么开始巩固知识:
设一企业合同约定每月5号发工资,请输出该企业今年实际每月发工资的日期(若5号是周六或周日,则工资提前到周五发)
1 System.DateTime dt = System.DateTime.Now;
2 dt = dt.AddDays(-(dt.DayOfYear - 1));
3 System.Console.WriteLine("该年第一天:{0}", dt.Date);
4 dt = dt.AddDays(4);
5 int iDay = 0;
6 System.Console.WriteLine("定位到第一个月的5号:{0}", dt.Date);
7 do
8 {
9 if (dt.Month != 5 && dt.Month != 10)
10 {
11 if (dt.DayOfWeek == System.DayOfWeek.Saturday)
12 { iDay = -1; }
13 if (dt.DayOfWeek == System.DayOfWeek.Sunday)
14 { iDay = -2; }
15 }
16 else
17 { iDay = -5; }
18 System.Console.WriteLine("{0}月的发薪水日期是{1}",
19 dt.Month, dt.AddDays(iDay).Date);
20 dt = dt.AddMonths(1);
21 iDay = 0;
22 }
23 while (dt.Year == System.DateTime.Now.Year);
2 dt = dt.AddDays(-(dt.DayOfYear - 1));
3 System.Console.WriteLine("该年第一天:{0}", dt.Date);
4 dt = dt.AddDays(4);
5 int iDay = 0;
6 System.Console.WriteLine("定位到第一个月的5号:{0}", dt.Date);
7 do
8 {
9 if (dt.Month != 5 && dt.Month != 10)
10 {
11 if (dt.DayOfWeek == System.DayOfWeek.Saturday)
12 { iDay = -1; }
13 if (dt.DayOfWeek == System.DayOfWeek.Sunday)
14 { iDay = -2; }
15 }
16 else
17 { iDay = -5; }
18 System.Console.WriteLine("{0}月的发薪水日期是{1}",
19 dt.Month, dt.AddDays(iDay).Date);
20 dt = dt.AddMonths(1);
21 iDay = 0;
22 }
23 while (dt.Year == System.DateTime.Now.Year);
设每年9.1开学,若9.1不是星期一,则要求开学日期改为9月的第一个星期一,请输出从1980-2004年的每年开学日期
1 //方案1
2 System.DateTime dt = new System.DateTime(1949, 9, 1);
3 System.DateTime tmpdt;
4 do
5 {
6 if (dt.DayOfWeek == System.DayOfWeek.Monday)
7 { tmpdt = dt; }
8 else
9 {
10 if (dt.DayOfWeek == System.DayOfWeek.Sunday)
11 { tmpdt = dt.AddDays(1); }
12 else
13 { tmpdt = dt.AddDays(7 - (int)dt.DayOfWeek + 1); }
14 }
15 System.Console.WriteLine("{0}年的9.1是{1}", dt.Year, dt.DayOfWeek);
16 System.Console.WriteLine("所以实际开学日期是:{0}", tmpdt.Date);
17 System.Console.WriteLine("------------------------");
18 dt = dt.AddYears(1);
19 }
20 while (dt.Year <= System.DateTime.Now.Year);
21
22
23 //方案2
24 System.DateTime dt = new System.DateTime(1949, 9, 1);
25 System.DateTime tmpdt;
26 do
27 {
28 if ((int)dt.DayOfWeek < 2)
29 { tmpdt = dt.AddDays(1 - (int)dt.DayOfWeek); }
30 else
31 {
32 tmpdt = dt.AddDays(7 - (int)dt.DayOfWeek + 1);
33 }
34 System.Console.WriteLine("{0}年的9.1是{1}", dt.Year, (int)dt.DayOfWeek);
35 System.Console.WriteLine("所以实际开学日期是:{0}", tmpdt.Date);
36 System.Console.WriteLine("------------------------");
37 dt = dt.AddYears(1);
38 }
39 while (dt.Year <= System.DateTime.Now.Year);
2 System.DateTime dt = new System.DateTime(1949, 9, 1);
3 System.DateTime tmpdt;
4 do
5 {
6 if (dt.DayOfWeek == System.DayOfWeek.Monday)
7 { tmpdt = dt; }
8 else
9 {
10 if (dt.DayOfWeek == System.DayOfWeek.Sunday)
11 { tmpdt = dt.AddDays(1); }
12 else
13 { tmpdt = dt.AddDays(7 - (int)dt.DayOfWeek + 1); }
14 }
15 System.Console.WriteLine("{0}年的9.1是{1}", dt.Year, dt.DayOfWeek);
16 System.Console.WriteLine("所以实际开学日期是:{0}", tmpdt.Date);
17 System.Console.WriteLine("------------------------");
18 dt = dt.AddYears(1);
19 }
20 while (dt.Year <= System.DateTime.Now.Year);
21
22
23 //方案2
24 System.DateTime dt = new System.DateTime(1949, 9, 1);
25 System.DateTime tmpdt;
26 do
27 {
28 if ((int)dt.DayOfWeek < 2)
29 { tmpdt = dt.AddDays(1 - (int)dt.DayOfWeek); }
30 else
31 {
32 tmpdt = dt.AddDays(7 - (int)dt.DayOfWeek + 1);
33 }
34 System.Console.WriteLine("{0}年的9.1是{1}", dt.Year, (int)dt.DayOfWeek);
35 System.Console.WriteLine("所以实际开学日期是:{0}", tmpdt.Date);
36 System.Console.WriteLine("------------------------");
37 dt = dt.AddYears(1);
38 }
39 while (dt.Year <= System.DateTime.Now.Year);
注意方案2的第29行,使用了01互换的方法,简化了周日和周一的计算
计算给定日期的年的每个月的周数
1 System.DateTime tmpdt = new DateTime(year, 1, 1);
2 int tmpMonth = tmpdt.Month;//记录当前月
3 int[] weeks = new int[12];//每个元素存放每月的星期数量
4 do
5 {
6 do
7 {//如果是周六就加1
8 if (tmpdt.DayOfWeek == System.DayOfWeek.Saturday)
9 {
10 weeks[tmpMonth - 1] = weeks[tmpMonth - 1] + 1;
11 }
12 tmpdt = tmpdt.AddDays(1);//下一天
13 }
14 while (tmpdt.Month == tmpMonth);//月循环
15 if (tmpdt.Day == 1 && tmpdt.DayOfWeek != System.DayOfWeek.Sunday)
16 {//如果每月的第一天不是周日,则上月再加一周
17 weeks[tmpMonth - 1] = weeks[tmpMonth - 1] + 1;
18 }
19 tmpMonth = tmpdt.Month;
20 }
21 while (tmpdt.Year == year);//年循环
22 for (int i = 0; i <= weeks.Length - 1; i++)
23 {
24 System.Console.WriteLine("{0}年 {1}月有{2}周", year, i + 1, weeks[i]);
25 }
2 int tmpMonth = tmpdt.Month;//记录当前月
3 int[] weeks = new int[12];//每个元素存放每月的星期数量
4 do
5 {
6 do
7 {//如果是周六就加1
8 if (tmpdt.DayOfWeek == System.DayOfWeek.Saturday)
9 {
10 weeks[tmpMonth - 1] = weeks[tmpMonth - 1] + 1;
11 }
12 tmpdt = tmpdt.AddDays(1);//下一天
13 }
14 while (tmpdt.Month == tmpMonth);//月循环
15 if (tmpdt.Day == 1 && tmpdt.DayOfWeek != System.DayOfWeek.Sunday)
16 {//如果每月的第一天不是周日,则上月再加一周
17 weeks[tmpMonth - 1] = weeks[tmpMonth - 1] + 1;
18 }
19 tmpMonth = tmpdt.Month;
20 }
21 while (tmpdt.Year == year);//年循环
22 for (int i = 0; i <= weeks.Length - 1; i++)
23 {
24 System.Console.WriteLine("{0}年 {1}月有{2}周", year, i + 1, weeks[i]);
25 }