计算两个日期间相差天数
今天要实现计算距某年某月某日还有多少天(距高考还有多少天),最后搞了半天写了两个方法。
1.上面两个函数都可以改为带参数的函数。
2.上面两个函数结果一样——205,不过没有验证。
3.感觉写的很不好,得优化。时间太紧了,只好先这样啦。
4.回头好好搞搞时间问题,时间转换怎么也转不过来。
1/************************
2//两个时间相减
3//小数减大数会出现负数
4//************************/
5protected void Button2_Click(object sender, EventArgs e)
6 {
7 DateTime d1 = new DateTime(2008, 6, 7);
8 DateTime d2 = new DateTime(2007, 11, 15);
9 string d3 = Convert.ToString(d1 - d2);//d3的值为 205.00:00:00
10
11 string[] str = d3.Split('.');//截取整数为相差天数
12 d3 = str[0];
13 Response.Write(d3);
14
15 }
16
17/***************************************
18//以结束时间为基数,例如高考时间(6-7).
19//分了三种情况:
20//小于6,大于6,等于6
21//***************************************/
22
23 private void CountDays()
24 {
25
26 /*计算距某年某月某日还有多少天(距高考还有多少天)
27 * nextYear int 高考年份
28 * days int 剩余天数
29 * */
30 int nowYear = DateTime.Now.Year;
31 int nowMonth = DateTime.Now.Month;
32 int nowDay = DateTime.Now.Day;
33
34 if (nowMonth > 6)//大于六月,则为下一年(nowYear+1)
35 {
36 //是否为闰年
37 if (DateTime.IsLeapYear(nowYear))
38 {
39 nextYear = nowYear + 1;
40 int days1 = 366 - DateTime.Now.DayOfYear;
41 DateTime dt = new DateTime(nowYear + 1, 6, 7);
42 int days2 = dt.DayOfYear;
43 days = days1 + days2;
44 }
45 else
46 {
47 nextYear = nowYear + 1;
48 int days1 = 365 - DateTime.Now.DayOfYear;
49 DateTime dt = new DateTime(nowYear + 1, 6, 7);
50 int days2 = dt.DayOfYear;
51 days = days1 + days2;
52 }
53 }
54
55 else if (nowMonth < 6)//小于六月,则为今年(nowYear)
56 {
57 //是否为闰年
58 if (DateTime.IsLeapYear(nowYear))
59 {
60 nextYear = nowYear;
61 int days1 = 366 - DateTime.Now.DayOfYear;
62
63 days = days1;
64 }
65 else
66 {
67 nextYear = nowYear;
68 int days1 = 365 - DateTime.Now.DayOfYear;
69
70 days = days1;
71 }
72 }
73 else if (nowMonth == 6)//等于六月,再判断日。
74 {
75 if (nowDay > 6)
76 {
77 //是否为闰年
78 if (DateTime.IsLeapYear(nowYear))
79 {
80 nextYear = nowYear + 1;
81 int days1 = 366 - DateTime.Now.DayOfYear;
82 DateTime dt = new DateTime(nowYear + 1, 6, 7);
83 int days2 = dt.DayOfYear;
84 days = days1 + days2;
85 }
86 else
87 {
88 nextYear = nowYear + 1;
89 int days1 = 365 - DateTime.Now.DayOfYear;
90 DateTime dt = new DateTime(nowYear + 1, 6, 7);
91 int days2 = dt.DayOfYear;
92 days = days1 + days2;
93 }
94 }
95 else if (nowDay < 6)
96 {
97 if (DateTime.IsLeapYear(nowYear))
98 {
99 nextYear = nowYear;
100 int days1 = 366 - DateTime.Now.DayOfYear;
101
102 days = days1;
103 }
104 else
105 {
106 nextYear = nowYear;
107 int days1 = 365 - DateTime.Now.DayOfYear;
108
109 days = days1;
110 }
111 }
112 else if (nowDay == 6)
113 {
114 days = 0;
115 }
116 }
117
118 }
2//两个时间相减
3//小数减大数会出现负数
4//************************/
5protected void Button2_Click(object sender, EventArgs e)
6 {
7 DateTime d1 = new DateTime(2008, 6, 7);
8 DateTime d2 = new DateTime(2007, 11, 15);
9 string d3 = Convert.ToString(d1 - d2);//d3的值为 205.00:00:00
10
11 string[] str = d3.Split('.');//截取整数为相差天数
12 d3 = str[0];
13 Response.Write(d3);
14
15 }
16
17/***************************************
18//以结束时间为基数,例如高考时间(6-7).
19//分了三种情况:
20//小于6,大于6,等于6
21//***************************************/
22
23 private void CountDays()
24 {
25
26 /*计算距某年某月某日还有多少天(距高考还有多少天)
27 * nextYear int 高考年份
28 * days int 剩余天数
29 * */
30 int nowYear = DateTime.Now.Year;
31 int nowMonth = DateTime.Now.Month;
32 int nowDay = DateTime.Now.Day;
33
34 if (nowMonth > 6)//大于六月,则为下一年(nowYear+1)
35 {
36 //是否为闰年
37 if (DateTime.IsLeapYear(nowYear))
38 {
39 nextYear = nowYear + 1;
40 int days1 = 366 - DateTime.Now.DayOfYear;
41 DateTime dt = new DateTime(nowYear + 1, 6, 7);
42 int days2 = dt.DayOfYear;
43 days = days1 + days2;
44 }
45 else
46 {
47 nextYear = nowYear + 1;
48 int days1 = 365 - DateTime.Now.DayOfYear;
49 DateTime dt = new DateTime(nowYear + 1, 6, 7);
50 int days2 = dt.DayOfYear;
51 days = days1 + days2;
52 }
53 }
54
55 else if (nowMonth < 6)//小于六月,则为今年(nowYear)
56 {
57 //是否为闰年
58 if (DateTime.IsLeapYear(nowYear))
59 {
60 nextYear = nowYear;
61 int days1 = 366 - DateTime.Now.DayOfYear;
62
63 days = days1;
64 }
65 else
66 {
67 nextYear = nowYear;
68 int days1 = 365 - DateTime.Now.DayOfYear;
69
70 days = days1;
71 }
72 }
73 else if (nowMonth == 6)//等于六月,再判断日。
74 {
75 if (nowDay > 6)
76 {
77 //是否为闰年
78 if (DateTime.IsLeapYear(nowYear))
79 {
80 nextYear = nowYear + 1;
81 int days1 = 366 - DateTime.Now.DayOfYear;
82 DateTime dt = new DateTime(nowYear + 1, 6, 7);
83 int days2 = dt.DayOfYear;
84 days = days1 + days2;
85 }
86 else
87 {
88 nextYear = nowYear + 1;
89 int days1 = 365 - DateTime.Now.DayOfYear;
90 DateTime dt = new DateTime(nowYear + 1, 6, 7);
91 int days2 = dt.DayOfYear;
92 days = days1 + days2;
93 }
94 }
95 else if (nowDay < 6)
96 {
97 if (DateTime.IsLeapYear(nowYear))
98 {
99 nextYear = nowYear;
100 int days1 = 366 - DateTime.Now.DayOfYear;
101
102 days = days1;
103 }
104 else
105 {
106 nextYear = nowYear;
107 int days1 = 365 - DateTime.Now.DayOfYear;
108
109 days = days1;
110 }
111 }
112 else if (nowDay == 6)
113 {
114 days = 0;
115 }
116 }
117
118 }
1.上面两个函数都可以改为带参数的函数。
2.上面两个函数结果一样——205,不过没有验证。
3.感觉写的很不好,得优化。时间太紧了,只好先这样啦。
4.回头好好搞搞时间问题,时间转换怎么也转不过来。
人的一生应该这样度过:当他回首往事的时候,不会因为虚度年华而悔恨,也不会因为碌碌无为而羞愧。