forked from siteserver/cms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDateUtils.cs
More file actions
336 lines (308 loc) · 10.9 KB
/
Copy pathDateUtils.cs
File metadata and controls
336 lines (308 loc) · 10.9 KB
1
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
using System;
using System.Globalization;
using SiteServer.Utils.Enumerations;
namespace SiteServer.Utils
{
public class DateUtils
{
private DateUtils()
{
}
public const string FormatStringDateTime = "yyyy-MM-dd HH:mm:ss";
public const string FormatStringDateOnly = "yyyy-MM-dd";
public static string GetRelatedDateTimeString(DateTime datetime)
{
string retval;
var interval = DateTime.Now - datetime;
if (interval.Days > 0)
{
if (interval.Days >= 7 && interval.Days < 35)
{
retval = $"{interval.Days/7}周";
}
else
{
retval = $"{interval.Days}天";
}
}
else if (interval.Hours > 0)
{
retval = $"{interval.Hours}小时";
}
else if (interval.Minutes > 0)
{
retval = $"{interval.Minutes}分钟";
}
else if (interval.Seconds > 0)
{
retval = $"{interval.Seconds}秒";
}
else if (interval.Milliseconds > 0)
{
retval = $"{interval.Milliseconds}毫秒";
}
else
{
retval = "1毫秒";
}
return retval;
}
public static string GetRelatedDateTimeString(DateTime datetime, string postfix)
{
return $"{GetRelatedDateTimeString(datetime)}{postfix}";
}
public static string GetDateAndTimeString(DateTime datetime, EDateFormatType dateFormat, ETimeFormatType timeFormat)
{
return $"{GetDateString(datetime, dateFormat)} {GetTimeString(datetime, timeFormat)}";
}
public static string GetDateAndTimeString(DateTime datetime)
{
if (datetime == SqlMinValue) return string.Empty;
return GetDateAndTimeString(datetime, EDateFormatType.Day, ETimeFormatType.ShortTime);
}
public static string GetDateString(DateTime datetime)
{
if (datetime == SqlMinValue) return string.Empty;
return GetDateString(datetime, EDateFormatType.Day);
}
public static string GetDateString(DateTime datetime, EDateFormatType dateFormat)
{
var format = string.Empty;
if (dateFormat == EDateFormatType.Year)
{
format = "yyyy年MM月";
}
else if (dateFormat == EDateFormatType.Month)
{
format = "MM月dd日";
}
else if (dateFormat == EDateFormatType.Day)
{
format = "yyyy-MM-dd";
}
else if (dateFormat == EDateFormatType.Chinese)
{
format = "yyyy年M月d日";
}
return datetime.ToString(format);
}
public static string GetTimeString(DateTime datetime)
{
return GetTimeString(datetime, ETimeFormatType.ShortTime);
}
public static string GetTimeString(DateTime datetime, ETimeFormatType timeFormat)
{
var retval = string.Empty;
if (timeFormat == ETimeFormatType.LongTime)
{
retval = datetime.ToLongTimeString();
}
else if (timeFormat == ETimeFormatType.ShortTime)
{
retval = datetime.ToShortTimeString();
}
return retval;
}
public static int GetSeconds(string intWithUnitString)
{
var seconds = 0;
if (!string.IsNullOrEmpty(intWithUnitString))
{
intWithUnitString = intWithUnitString.Trim().ToLower();
if (intWithUnitString.EndsWith("h"))
{
seconds = 60 * 60 * TranslateUtils.ToInt(intWithUnitString.TrimEnd('h'));
}
else if (intWithUnitString.EndsWith("m"))
{
seconds = 60 * TranslateUtils.ToInt(intWithUnitString.TrimEnd('m'));
}
else if (intWithUnitString.EndsWith("s"))
{
seconds = TranslateUtils.ToInt(intWithUnitString.TrimEnd('s'));
}
else
{
seconds = TranslateUtils.ToInt(intWithUnitString);
}
}
return seconds;
}
public static bool IsSince(string val)
{
if (!string.IsNullOrEmpty(val))
{
val = val.Trim().ToLower();
if (val.EndsWith("y") || val.EndsWith("m") || val.EndsWith("d") || val.EndsWith("h"))
{
return true;
}
}
return false;
}
public static int GetSinceHours(string intWithUnitString)
{
var hours = 0;
if (!string.IsNullOrEmpty(intWithUnitString))
{
intWithUnitString = intWithUnitString.Trim().ToLower();
if (intWithUnitString.EndsWith("y"))
{
hours = 8760 * TranslateUtils.ToInt(intWithUnitString.TrimEnd('y'));
}
else if (intWithUnitString.EndsWith("m"))
{
hours = 720 * TranslateUtils.ToInt(intWithUnitString.TrimEnd('m'));
}
else if (intWithUnitString.EndsWith("d"))
{
hours = 24 * TranslateUtils.ToInt(intWithUnitString.TrimEnd('d'));
}
else if (intWithUnitString.EndsWith("h"))
{
hours = TranslateUtils.ToInt(intWithUnitString.TrimEnd('h'));
}
else
{
hours = TranslateUtils.ToInt(intWithUnitString);
}
}
return hours;
}
public static bool IsTheSameDay(DateTime d1, DateTime d2)
{
if (d1.Year == d2.Year && d1.Month == d2.Month && d1.Day == d2.Day)
{
return true;
}
return false;
}
public static string Format(DateTime datetime, string formatString)
{
string retval;
if (!string.IsNullOrEmpty(formatString))
{
retval = formatString.IndexOf("{0:", StringComparison.Ordinal) != -1 ? string.Format(DateTimeFormatInfo.InvariantInfo, formatString, datetime) : datetime.ToString(formatString, DateTimeFormatInfo.InvariantInfo);
}
else
{
retval = GetDateString(datetime);
}
return retval;
}
public static DateTime SqlMinValue => new DateTime(1754, 1, 1, 0, 0, 0, 0);
//Task used
public static int GetDayOfWeek(DateTime dateTime)
{
switch (dateTime.DayOfWeek)
{
case DayOfWeek.Monday:
return 1;
case DayOfWeek.Tuesday:
return 2;
case DayOfWeek.Wednesday:
return 3;
case DayOfWeek.Thursday:
return 4;
case DayOfWeek.Friday:
return 5;
case DayOfWeek.Saturday:
return 6;
default:
return 7;
}
}
public static string ParseThisMoment(DateTime dateTime)
{
if (dateTime != SqlMinValue)
{
return ParseThisMoment(dateTime, DateTime.Now);
}
return string.Empty;
}
/// <summary>
/// 把两个时间差,三天内的时间用今天,昨天,前天表示,后跟时间,无日期
/// </summary>
public static string ParseThisMoment(DateTime dateTime, DateTime currentDateTime)
{
string result;
if (currentDateTime.Year == dateTime.Year && currentDateTime.Month == dateTime.Month)//如果date和当前时间年份或者月份不一致,则直接返回"yyyy-MM-dd HH:mm"格式日期
{
if (DateDiff("hour", dateTime, currentDateTime) <= 10)//如果date和当前时间间隔在10小时内(曾经是3小时)
{
if (DateDiff("hour", dateTime, currentDateTime) > 0)
return DateDiff("hour", dateTime, currentDateTime) + "小时前";
if (DateDiff("minute", dateTime, currentDateTime) > 0)
return DateDiff("minute", dateTime, currentDateTime) + "分钟前";
if (DateDiff("second", dateTime, currentDateTime) >= 0)
return DateDiff("second", dateTime, currentDateTime) + "秒前";
else
return "刚才";//为了解决时间精度不够导致发帖时间问题的兼容
}
else
{
switch (currentDateTime.Day - dateTime.Day)
{
case 0:
result = "今天 " + dateTime.ToString("HH") + ":" + dateTime.ToString("mm");
break;
case 1:
result = "昨天 " + dateTime.ToString("HH") + ":" + dateTime.ToString("mm");
break;
case 2:
result = "前天 " + dateTime.ToString("HH") + ":" + dateTime.ToString("mm");
break;
default:
result = dateTime.ToString("yyyy-MM-dd HH:mm");
break;
}
}
}
else
result = dateTime.ToString("yyyy-MM-dd HH:mm");
return result;
}
/// <summary>
/// 两个时间的差值,可以为秒,小时,天,分钟
/// </summary>
/// <returns></returns>
public static long DateDiff(string interval, DateTime startDate, DateTime endDate)
{
long retval = 0;
var ts = new TimeSpan(endDate.Ticks - startDate.Ticks);
if (interval == "second")
{
retval = (long)ts.TotalSeconds;
}
else if (interval == "minute")
{
retval = (long) ts.TotalMinutes;
}
else if (interval == "hour")
{
retval = (long) ts.TotalHours;
}
else if (interval == "day")
{
retval = ts.Days;
}
else if (interval == "week")
{
retval = ts.Days / 7;
}
else if (interval == "month")
{
retval = ts.Days / 30;
}
else if (interval == "quarter")
{
retval = ts.Days / 30 / 3;
}
else if (interval == "year")
{
retval = ts.Days / 365;
}
return retval;
}
}
}