DateHelper.cs
8.77 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
using System;
using System.Collections.Generic;
using System.Text;
namespace Hh.Mes.Common.Date
{
public class DateHelper
{
/// <summary>
/// 取得某月的第一天
/// </summary>
/// <param name="datetime">要取得月份第一天的时间</param>
/// <returns></returns>
public static DateTime FirstDayOfMonth(DateTime datetime)
{
return datetime.AddDays(1 - datetime.Day);
}
/// <summary>
/// 取得某月的最后一天
/// </summary>
/// <param name="datetime"></param>
/// <returns></returns>
public static DateTime LastDayOfMonth(DateTime datetime)
{
return datetime.AddDays(1 - datetime.Day).AddMonths(1).AddDays(-1);
}
/// <summary>
/// 取得上个月第一天
/// </summary>
/// <param name="datetime">要取得上个月第一天的当前时间</param>
/// <returns></returns>
public static DateTime FirstDayOfPreviousMonth(DateTime datetime)
{
return datetime.AddDays(1 - datetime.Day).AddMonths(-1);
}
/// <summary>
/// 取得上个月的最后一天
/// </summary>
/// <param name="datetime">要取得上个月最后一天的当前时间</param>
/// <returns></returns>
public static DateTime LastDayOfPrdviousMonth(DateTime datetime)
{
return datetime.AddDays(1 - datetime.Day).AddDays(-1);
}
/// <summary>
/// 把秒转换成分钟
/// </summary>
/// <returns></returns>
public static int SecondToMinute(int Second)
{
decimal mm = (decimal)((decimal)Second / (decimal)60);
return Convert.ToInt32(Math.Ceiling(mm));
}
#region 返回某年某月最后一天
/// <summary>
/// 返回某年某月最后一天
/// </summary>
/// <param name="year">年份</param>
/// <param name="month">月份</param>
/// <returns>日</returns>
public static int GetMonthLastDate(int year, int month)
{
DateTime lastDay = new DateTime(year, month, new System.Globalization.GregorianCalendar().GetDaysInMonth(year, month));
int Day = lastDay.Day;
return Day;
}
#endregion
#region 返回时间差
public static string DateDiff(DateTime DateTime1, DateTime DateTime2)
{
string dateDiff = null;
try
{
//TimeSpan ts1 = new TimeSpan(DateTime1.Ticks);
//TimeSpan ts2 = new TimeSpan(DateTime2.Ticks);
//TimeSpan ts = ts1.Subtract(ts2).Duration();
TimeSpan ts = DateTime2 - DateTime1;
if (ts.Days >= 1)
{
dateDiff = DateTime1.Month.ToString() + "月" + DateTime1.Day.ToString() + "日";
}
else
{
if (ts.Hours > 1)
{
dateDiff = ts.Hours.ToString() + "小时前";
}
else
{
dateDiff = ts.Minutes.ToString() + "分钟前";
}
}
}
catch
{ }
return dateDiff;
}
#endregion
#region 获得两个日期的间隔
/// <summary>
/// 获得两个日期的间隔绝对值
/// </summary>
/// <param name="DateTime1">日期一。</param>
/// <param name="DateTime2">日期二。</param>
/// <returns>日期间隔TimeSpan。</returns>
public static int DateDiff2(DateTime DateTime1, DateTime DateTime2)
{
TimeSpan ts1 = new TimeSpan(DateTime1.Ticks);
TimeSpan ts2 = new TimeSpan(DateTime2.Ticks);
TimeSpan ts = ts1.Subtract(ts2).Duration();
return (int)ts.TotalMinutes;
}
/// <summary>
/// 获得两个日期的间隔绝对值
/// </summary>
/// <param name="DateTime1">日期一。</param>
/// <param name="DateTime2">日期二。</param>
/// <returns>日期间隔TimeSpan。</returns>
public static int DateDiff3(TimeSpan DateTime1, TimeSpan DateTime2)
{
TimeSpan ts1 = new TimeSpan(DateTime1.Ticks);
TimeSpan ts2 = new TimeSpan(DateTime2.Ticks);
TimeSpan ts = ts1.Subtract(ts2).Duration();
return (int)ts.TotalMinutes;
}
/// <summary>
/// 获得两个日期的间隔绝对值
/// </summary>
/// <param name="DateTime1">日期一。</param>
/// <param name="DateTime2">日期二。</param>
/// <returns>日期间隔TimeSpan。</returns>
public static int DateMinuter(TimeSpan DateTime)
{
TimeSpan ts = new TimeSpan(DateTime.Ticks);
var hours = ts.Hours * 60;
var min = ts.Minutes;
var sum = hours + min;
return sum;
}
#endregion
#region 格式化日期时间
/// <summary>
/// 格式化日期时间
/// </summary>
/// <param name="dateTime1">日期时间</param>
/// <param name="dateMode">显示模式</param>
/// <returns>0-9种模式的日期</returns>
public static string FormatDate(DateTime dateTime1, string dateMode)
{
switch (dateMode)
{
case "0":
return dateTime1.ToString("yyyy-MM-dd");
case "1":
return dateTime1.ToString("yyyy-MM-dd HH:mm:ss");
case "2":
return dateTime1.ToString("yyyy/MM/dd");
case "3":
return dateTime1.ToString("yyyy年MM月dd日");
case "4":
return dateTime1.ToString("MM-dd");
case "5":
return dateTime1.ToString("MM/dd");
case "6":
return dateTime1.ToString("MM月dd日");
case "7":
return dateTime1.ToString("yyyy-MM");
case "8":
return dateTime1.ToString("yyyy/MM");
case "9":
return dateTime1.ToString("yyyy年MM月");
default:
return dateTime1.ToString();
}
}
#endregion
#region 得到随机日期
/// <summary>
/// 得到随机日期
/// </summary>
/// <param name="time1">起始日期</param>
/// <param name="time2">结束日期</param>
/// <returns>间隔日期之间的 随机日期</returns>
public static DateTime GetRandomTime(DateTime time1, DateTime time2)
{
Random random = new Random();
DateTime minTime = new DateTime();
DateTime maxTime = new DateTime();
System.TimeSpan ts = new System.TimeSpan(time1.Ticks - time2.Ticks);
// 获取两个时间相隔的秒数
double dTotalSecontds = ts.TotalSeconds;
int iTotalSecontds = 0;
if (dTotalSecontds > System.Int32.MaxValue)
{
iTotalSecontds = System.Int32.MaxValue;
}
else if (dTotalSecontds < System.Int32.MinValue)
{
iTotalSecontds = System.Int32.MinValue;
}
else
{
iTotalSecontds = (int)dTotalSecontds;
}
if (iTotalSecontds > 0)
{
minTime = time2;
maxTime = time1;
}
else if (iTotalSecontds < 0)
{
minTime = time1;
maxTime = time2;
}
else
{
return time1;
}
int maxValue = iTotalSecontds;
if (iTotalSecontds <= System.Int32.MinValue)
maxValue = System.Int32.MinValue + 1;
int i = random.Next(System.Math.Abs(maxValue));
return minTime.AddSeconds(i);
}
public static decimal DateDiff(DateTime timeSpanStart, DateTime? timeSpanEnd)
{
throw new NotImplementedException();
}
#endregion
public static int DateDiffDay(DateTime? dateStart, DateTime? dateEnd)
{
if (dateStart == null || dateEnd == null) return 0;
var start = Convert.ToDateTime(dateStart.Value.ToShortDateString());
var end = Convert.ToDateTime(dateEnd.Value.ToShortDateString());
var sp = end.Subtract(start);
return sp.Days;
}
}
}