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