asp.net(C#)实现功能强大的时间日期处理类完整实例

时间:2022-09-14 10:27:41

本文实例讲述了asp.net(C#)实现功能强大的时间日期处理类。分享给大家供大家参考,具体如下:

?
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
namespace MSCL
{
  /// <summary>
  /// 时间日期处理类
  /// </summary>
  public class DateTimeHelper
  {
    #region 返回本年有多少天
    /// <summary>返回本年有多少天</summary>
    /// <param name="iYear">年份</param>
    /// <returns>本年的天数</returns>
    public static int GetDaysOfYear(int iYear)
    {
      int cnt = 0;
      if (IsRuYear(iYear))
      {
        //闰年多 1 天 即:2 月为 29 天
        cnt = 366;
      }
      else
      {
        //--非闰年少1天 即:2 月为 28 天
        cnt = 365;
      }
      return cnt;
    }
    #endregion
    #region 返回本年有多少天
    /// <summary>本年有多少天</summary>
    /// <param name="dt">日期</param>
    /// <returns>本天在当年的天数</returns>
    public static int GetDaysOfYear(DateTime idt)
    {
      int n;
      //取得传入参数的年份部分,用来判断是否是闰年
      n = idt.Year;
      if (IsRuYear(n))
      {
        //闰年多 1 天 即:2 月为 29 天
        return 366;
      }
      else
      {
        //--非闰年少1天 即:2 月为 28 天
        return 365;
      }
    }
    #endregion
    #region 返回本月有多少天
    /// <summary>本月有多少天</summary>
    /// <param name="iYear">年</param>
    /// <param name="Month">月</param>
    /// <returns>天数</returns>
    public static int GetDaysOfMonth(int iYear, int Month)
    {
      int days = 0;
      switch (Month)
      {
        case 1:
          days = 31;
          break;
        case 2:
          if (IsRuYear(iYear))
          {
            //闰年多 1 天 即:2 月为 29 天
            days = 29;
          }
          else
          {
            //--非闰年少1天 即:2 月为 28 天
            days = 28;
          }
          break;
        case 3:
          days = 31;
          break;
        case 4:
          days = 30;
          break;
        case 5:
          days = 31;
          break;
        case 6:
          days = 30;
          break;
        case 7:
          days = 31;
          break;
        case 8:
          days = 31;
          break;
        case 9:
          days = 30;
          break;
        case 10:
          days = 31;
          break;
        case 11:
          days = 30;
          break;
        case 12:
          days = 31;
          break;
      }
      return days;
    }
    #endregion
    #region 返回本月有多少天
    /// <summary>本月有多少天</summary>
    /// <param name="dt">日期</param>
    /// <returns>天数</returns>
    public static int GetDaysOfMonth(DateTime dt)
    {
      //--------------------------------//
      //--从dt中取得当前的年,月信息 --//
      //--------------------------------//
      int year, month, days = 0;
      year = dt.Year;
      month = dt.Month;
      //--利用年月信息,得到当前月的天数信息。
      switch (month)
      {
        case 1:
          days = 31;
          break;
        case 2:
          if (IsRuYear(year))
          {
            //闰年多 1 天 即:2 月为 29 天
            days = 29;
          }
          else
          {
            //--非闰年少1天 即:2 月为 28 天
            days = 28;
          }
          break;
        case 3:
          days = 31;
          break;
        case 4:
          days = 30;
          break;
        case 5:
          days = 31;
          break;
        case 6:
          days = 30;
          break;
        case 7:
          days = 31;
          break;
        case 8:
          days = 31;
          break;
        case 9:
          days = 30;
          break;
        case 10:
          days = 31;
          break;
        case 11:
          days = 30;
          break;
        case 12:
          days = 31;
          break;
      }
      return days;
    }
    #endregion
    #region 返回当前日期的星期名称
    /// <summary>返回当前日期的星期名称</summary>
    /// <param name="dt">日期</param>
    /// <returns>星期名称</returns>
    public static string GetWeekNameOfDay(DateTime idt)
    {
      string dt, week = "";
      dt = idt.DayOfWeek.ToString();
      switch (dt)
      {
        case "Mondy":
          week = "星期一";
          break;
        case "Tuesday":
          week = "星期二";
          break;
        case "Wednesday":
          week = "星期三";
          break;
        case "Thursday":
          week = "星期四";
          break;
        case "Friday":
          week = "星期五";
          break;
        case "Saturday":
          week = "星期六";
          break;
        case "Sunday":
          week = "星期日";
          break;
      }
      return week;
    }
    #endregion
    #region 返回当前日期的星期编号
    /// <summary>返回当前日期的星期编号</summary>
    /// <param name="dt">日期</param>
    /// <returns>星期数字编号</returns>
    public static string GetWeekNumberOfDay(DateTime idt)
    {
      string dt, week = "";
      dt = idt.DayOfWeek.ToString();
      switch (dt)
      {
        case "Mondy":
          week = "1";
          break;
        case "Tuesday":
          week = "2";
          break;
        case "Wednesday":
          week = "3";
          break;
        case "Thursday":
          week = "4";
          break;
        case "Friday":
          week = "5";
          break;
        case "Saturday":
          week = "6";
          break;
        case "Sunday":
          week = "7";
          break;
      }
      return week;
    }
    #endregion
    #region 判断当前日期所属的年份是否是闰年,私有函数
    /// <summary>判断当前日期所属的年份是否是闰年,私有函数</summary>
    /// <param name="dt">日期</param>
    /// <returns>是闰年:True ,不是闰年:False</returns>
    private static bool IsRuYear(DateTime idt)
    {
      //形式参数为日期类型
      //例如:2003-12-12
      int n;
      n = idt.Year;
      if ((n % 400 == 0) || (n % 4 == 0 && n % 100 != 0))
      {
        return true;
      }
      else
      {
        return false;
      }
    }
    #endregion
    #region 判断当前年份是否是闰年,私有函数
    /// <summary>判断当前年份是否是闰年,私有函数</summary>
    /// <param name="dt">年份</param>
    /// <returns>是闰年:True ,不是闰年:False</returns>
    private static bool IsRuYear(int iYear)
    {
      //形式参数为年份
      //例如:2003
      int n;
      n = iYear;
      if ((n % 400 == 0) || (n % 4 == 0 && n % 100 != 0))
      {
        return true;
      }
      else
      {
        return false;
      }
    }
    #endregion
    #region 将输入的字符串转化为日期。如果字符串的格式非法,则返回当前日期
    /// <summary>
    /// 将输入的字符串转化为日期。如果字符串的格式非法,则返回当前日期。
    /// </summary>
    /// <param name="strInput">输入字符串</param>
    /// <returns>日期对象</returns>
    public static DateTime ConvertStringToDate(string strInput)
    {
      DateTime oDateTime;
      try
      {
        oDateTime = DateTime.Parse(strInput);
      }
      catch (Exception)
      {
        oDateTime = DateTime.Today;
      }
      return oDateTime;
    }
    #endregion
    #region 将日期对象转化为格式字符串
    /// <summary>
    /// 将日期对象转化为格式字符串
    /// </summary>
    /// <param name="oDateTime">日期对象</param>
    /// <param name="strFormat">
    /// 格式:
    ///    "SHORTDATE"===短日期
    ///    "LONGDATE"==长日期
    ///    其它====自定义格式
    /// </param>
    /// <returns>日期字符串</returns>
    public static string ConvertDateToString(DateTime oDateTime, string strFormat)
    {
      string strDate = "";
      try
      {
        switch (strFormat.ToUpper())
        {
          case "SHORTDATE":
            strDate = oDateTime.ToShortDateString();
            break;
          case "LONGDATE":
            strDate = oDateTime.ToLongDateString();
            break;
          default:
            strDate = oDateTime.ToString(strFormat);
            break;
        }
      }
      catch (Exception)
      {
        strDate = oDateTime.ToShortDateString();
      }
      return strDate;
    }
    #endregion
    #region 判断是否为合法日期,必须大于1800年1月1日
    /// <summary>
    /// 判断是否为合法日期,必须大于1800年1月1日
    /// </summary>
    /// <param name="strDate">输入日期字符串</param>
    /// <returns>True/False</returns>
    public static bool IsDateTime(string strDate)
    {
      try
      {
        DateTime oDate = DateTime.Parse(strDate);
        if (oDate.CompareTo(DateTime.Parse("1800-1-1")) > 0)
          return true;
        else
          return false;
      }
      catch (Exception)
      {
        return false;
      }
    }
    #endregion
    #region 获取两个日期之间的差值 可返回年 月 日 小时 分钟 秒
    /// <summary>
    /// 获取两个日期之间的差值
    /// </summary>
    /// <param name="howtocompare">比较的方式可为:year month day hour minute second</param>
    /// <param name="startDate">开始日期</param>
    /// <param name="endDate">结束日期</param>
    /// <returns>时间差</returns>
    public static double DateDiff(string howtocompare, DateTime startDate, DateTime endDate)
    {
      double diff = 0;
      try
      {
        TimeSpan TS = new TimeSpan(endDate.Ticks - startDate.Ticks);
        switch (howtocompare.ToLower())
        {
          case "year":
            diff = Convert.ToDouble(TS.TotalDays / 365);
            break;
          case "month":
            diff = Convert.ToDouble((TS.TotalDays / 365) * 12);
            break;
          case "day":
            diff = Convert.ToDouble(TS.TotalDays);
            break;
          case "hour":
            diff = Convert.ToDouble(TS.TotalHours);
            break;
          case "minute":
            diff = Convert.ToDouble(TS.TotalMinutes);
            break;
          case "second":
            diff = Convert.ToDouble(TS.TotalSeconds);
            break;
        }
      }
      catch (Exception)
      {
        diff = 0;
      }
      return diff;
    }
    #endregion
    #region 计算两个日期之间相差的工作日天数
    /// <summary>
    /// 计算两个日期之间相差的工作日天数
    /// </summary>
    /// <param name="dtStart">开始日期</param>
    /// <param name="dtEnd">结束日期</param>
    /// <param name="Flag">是否除去周六,周日</param>
    /// <returns>Int</returns>
    public static int CalculateWorkingDays(DateTime dtStart, DateTime dtEnd, bool Flag)
    {
      int count = 0;
      for (DateTime dtTemp = dtStart; dtTemp < dtEnd; dtTemp = dtTemp.AddDays(1))
      {
        if (Flag)
        {
          if (dtTemp.DayOfWeek != DayOfWeek.Saturday && dtTemp.DayOfWeek != DayOfWeek.Sunday)
          {
            count++;
          }
        }
        else
        {
          count++;
        }
      }
      return count;
    }
    #endregion
  }
}

希望本文所述对大家asp.net程序设计有所帮助。