1
using System;
2
using System.Collections.Generic;
3
using System.Text;
4
5
namespace BLL
6

{
7
/**//// <summary>
8
/// 時(shí)間幫助工具類
9
/// </summary>
10
class DateTimeUtils
11
{
12
13
根據(jù)時(shí)間返回幾個(gè)月前,幾天前,幾小時(shí)前,幾分鐘前,幾秒前#region 根據(jù)時(shí)間返回幾個(gè)月前,幾天前,幾小時(shí)前,幾分鐘前,幾秒前
14
15
/**//// <summary>
16
/// 根據(jù)時(shí)間返回幾個(gè)月前,幾天前,幾小時(shí)前,幾分鐘前,幾秒前
17
/// </summary>
18
/// <param name="dt"></param>
19
/// <returns></returns>
20
public static string DateStringFromNow(DateTime dt)
21
{
22
TimeSpan span = DateTime.Now - dt;
23
if (span.TotalDays > 60)
24
{
25
return dt.ToShortDateString();
26
}
27
else if (span.TotalDays > 30)
28
{
29
return "1個(gè)月前";
30
}
31
else if (span.TotalDays > 14)
32
{
33
return "2周前";
34
}
35
else if (span.TotalDays > 7)
36
{
37
return "1周前";
38
}
39
else if (span.TotalDays > 1)
40
{
41
return string.Format("{0}天前", (int)Math.Floor(span.TotalDays));
42
}
43
else if (span.TotalHours > 1)
44
{
45
return string.Format("{0}小時(shí)前", (int)Math.Floor(span.TotalHours));
46
}
47
else if (span.TotalMinutes > 1)
48
{
49
return string.Format("{0}分鐘前", (int)Math.Floor(span.TotalMinutes));
50
}
51
else if (span.TotalSeconds >= 1)
52
{
53
return string.Format("{0}秒前", (int)Math.Floor(span.TotalSeconds));
54
}
55
else
56
{
57
return "1秒前";
58
}
59
}
60
61
#endregion
62
63
時(shí)間差計(jì)算#region 時(shí)間差計(jì)算
64
65
/**//// <summary>
66
/// 計(jì)算兩個(gè)時(shí)間的差值,返回的是x天x小時(shí)x分鐘x秒
67
/// </summary>
68
/// <param name="DateTime1"></param>
69
/// <param name="DateTime2"></param>
70
/// <returns></returns>
71
public static string DateDiff(DateTime DateTime1, DateTime DateTime2)
72
{
73
string dateDiff = null;
74
TimeSpan ts1 = new TimeSpan(DateTime1.Ticks);
75
TimeSpan ts2 = new TimeSpan(DateTime2.Ticks);
76
TimeSpan ts = ts1.Subtract(ts2).Duration();
77
//TimeSpan ts=ts1.Add(ts2).Duration();
78
dateDiff = ts.Days.ToString() + "天" + ts.Hours.ToString() + "小時(shí)" + ts.Minutes.ToString() + "分鐘" + ts.Seconds.ToString() + "秒";
79
return dateDiff;
80
}
81
82
/**//// <summary>
83
/// 時(shí)間相差值,返回時(shí)間差
84
/// 調(diào)用時(shí),isTotal為true時(shí),返回的時(shí)帶小數(shù)的天數(shù),否則返回的是整數(shù)
85
/// </summary>
86
/// <param name="DateTime1"></param>
87
/// <param name="DateTime2"></param>
88
/// <param name="isTotal"></param>
89
/// <returns></returns>
90
public static string DateDiff(DateTime DateTime1, DateTime DateTime2, bool isTotal)
91
{
92
TimeSpan ts = DateTime2 - DateTime1;
93
if (isTotal)
94
//帶小數(shù)的天數(shù),比如1天12小時(shí)結(jié)果就是1.5
95
return ts.TotalDays.ToString();
96
else
97
//整數(shù)天數(shù),1天12小時(shí)或者1天20小時(shí)結(jié)果都是1
98
return ts.Days.ToString();
99
}
100
101
#endregion
102
103
日期比較#region 日期比較
104
105
/**//// <summary>
106
/// 日期比較
107
/// </summary>
108
/// <param name="today">當(dāng)前日期</param>
109
/// <param name="writeDate">輸入日期</param>
110
/// <param name="n">比較天數(shù)</param>
111
/// <returns>大于天數(shù)返回true,小于返回false</returns>
112
public static bool CompareDate(string today, string writeDate, int n)
113
{
114
DateTime Today = Convert.ToDateTime(today);
115
DateTime WriteDate = Convert.ToDateTime(writeDate);
116
WriteDate = WriteDate.AddDays(n);
117
if (Today >= WriteDate)
118
return false;
119
else
120
return true;
121
}
122
123
#endregion
124
125
根據(jù)英文的星期幾返回中文的星期幾#region 根據(jù)英文的星期幾返回中文的星期幾
126
127
/**//// <summary>
128
/// 根據(jù)英文的星期幾返回中文的星期幾
129
/// 如WhichDay("Sunday"),返回星期日
130
/// </summary>
131
/// <param name="enWeek"></param>
132
/// <returns></returns>
133
public static string WhichDay(string enWeek)
134
{
135
switch (enWeek.Trim())
136
{
137
case "Sunday":
138
return "星期日";
139
case "Monday":
140
return "星期一";
141
case "Tuesday":
142
return "星期二";
143
case "Wednesday":
144
return "星期三";
145
case "Thursday":
146
return "星期四";
147
case "Friday":
148
return "星期五";
149
case "Saturday":
150
return "星期六";
151
default:
152
return enWeek;
153
}
154
}
155
156
#endregion
157
158
生日提醒#region 生日提醒
159
160
/**//// <summary>
161
/// 根據(jù)出生年月進(jìn)行生日提醒
162
/// </summary>
163
/// <param name="birthday"></param>
164
/// <returns></returns>
165
public static string GetBirthdayTip(DateTime birthday)
166
{
167
DateTime now = DateTime.Now;
168
//TimeSpan span = DateTime.Now - birthday;
169
int nowMonth = now.Month;
170
int birtMonth = birthday.Month;
171
if (nowMonth == 12 && birtMonth == 1)
172
return string.Format("下月{0}號(hào)", birthday.Day);
173
if (nowMonth == 1 && birtMonth == 12)
174
return string.Format("上月{0}號(hào)", birthday.Day);
175
int months = now.Month - birthday.Month;
176
//int days = now.Day - birthday.Day;
177
if (months == 1)
178
return string.Format("上月{0}號(hào)", birthday.Day);
179
else if (months == -1)
180
return string.Format("下月{0}號(hào)", birthday.Day);
181
else if (months == 0)
182
{
183
if (now.Day == birthday.Day)
184
return "今天";
185
return string.Format("本月{0}號(hào)", birthday.Day);
186
}
187
else if (months > 1)
188
return string.Format("已過{0}月", months);
189
else
190
return string.Format("{0}月{1}日", birthday.Month, birthday.Day);
191
}
192
193
#endregion
194
195
}
196
}
197

2

3

4

5

6



7


8

9

10

11



12

13


14

15


16

17

18

19

20

21



22

23

24



25

26

27

28



29

30

31

32



33

34

35

36



37

38

39

40



41

42

43

44



45

46

47

48



49

50

51

52



53

54

55

56



57

58

59

60

61

62

63


64

65


66

67

68

69

70

71

72



73

74

75

76

77

78

79

80

81

82


83

84

85

86

87

88

89

90

91



92

93

94

95

96

97

98

99

100

101

102

103


104

105


106

107

108

109

110

111

112

113



114

115

116

117

118

119

120

121

122

123

124

125


126

127


128

129

130

131

132

133

134



135

136



137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158


159

160


161

162

163

164

165

166



167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182



183

184

185

186

187

188

189

190

191

192

193

194

195

196

197
