DateTimeExtensions.cs
5.61 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
using System;
using System.Globalization;
using System.Security.Cryptography;
using System.Text;
namespace Infrastructure
{
public static class DateTimeExtensions
{
public static string GetHourly(this DateTime datevalue)
{
return datevalue.ToString("yyyy/MM/dd HH");
}
public static string GetDaily(this DateTime datevalue)
{
return datevalue.ToString("yyyy/MM/dd");
}
public static string GetWeekly(this DateTime datevalue)
{
GregorianCalendar gc = new GregorianCalendar();
int weekOfYear = gc.GetWeekOfYear(datevalue, CalendarWeekRule.FirstDay, DayOfWeek.Monday);
return "W" + weekOfYear.ToString();
}
public static string GetMonthly(this DateTime datevalue)
{
return datevalue.ToString("yyyy/MM");
}
public static string GetEnMonthly(this DateTime datevalue)
{
string rtn = datevalue.Year.ToString().Substring(2, 2);
string month1 = "";
switch (datevalue.Month.ToString())
{
case "1":
month1 = "Jan";
break;
case "2":
month1 = "Feb";
break;
case "3":
month1 = "Mar";
break;
case "4":
month1 = "Apr";
break;
case "5":
month1 = "May";
break;
case "6":
month1 = "June";
break;
case "7":
month1 = "July";
break;
case "8":
month1 = "Aug";
break;
case "9":
month1 = "Sept";
break;
case "10":
month1 = "Oct";
break;
case "11":
month1 = "Nov";
break;
case "12":
month1 = "Dec";
break;
}
return month1 + "-" + rtn;
}
public static string GetWeeklyStart(this DateTime datevalue)
{
int dayInWeek = (int)datevalue.DayOfWeek;
if (dayInWeek == 0) { dayInWeek = 7; }
return datevalue.AddDays(-1 * (dayInWeek - 1)).GetDaily();
}
public static string GetWeeklyEnd(this DateTime datevalue)
{
int dayInWeek = (int)datevalue.DayOfWeek;
if (dayInWeek == 0) { dayInWeek = 7; }
return datevalue.AddDays(-1 * (dayInWeek - 1)).AddDays(6).GetDaily();
}
public static string GetFormat(this DateTime datevalue, string dateType)
{
string rtn = datevalue.ToString("yyyy/MM/dd HH:mm:ss");
switch (dateType.ToLower())
{
case "hourly":
rtn = datevalue.GetHourly();
break;
case "daily":
rtn = datevalue.GetDaily();
break;
case "weekly":
rtn = datevalue.GetWeekly();
break;
case "monthly":
rtn = datevalue.GetMonthly();
break;
case "weeklystart":
rtn = datevalue.GetWeeklyStart();
break;
case "weeklyend":
rtn = datevalue.GetWeeklyEnd();
break;
default:
break;
}
return rtn;
}
public static int GetTimeSpanDays(this DateTime datevalue1, DateTime datevalue2)
{
int rtn;
TimeSpan timeSpan = datevalue2.Subtract(datevalue1);
rtn = timeSpan.Days;
return rtn;
}
public static int GetTimeSpanHours(this DateTime datevalue1, DateTime datevalue2)
{
int rtn;
TimeSpan timeSpan = datevalue2.Subtract(datevalue1);
rtn = timeSpan.Days * 24 + timeSpan.Hours;
return rtn;
}
public static int GetTimeSpanMinutes(this DateTime datevalue1, DateTime datevalue2)
{
int rtn;
TimeSpan timeSpan = datevalue2.Subtract(datevalue1);
rtn = (timeSpan.Days * 24 + timeSpan.Hours) * 60 + timeSpan.Minutes;
return rtn;
}
public static int GetTimeSpanSeconds(this DateTime datevalue1, DateTime datevalue2)
{
int rtn;
TimeSpan timeSpan = datevalue2.Subtract(datevalue1);
rtn = ((timeSpan.Days * 24 + timeSpan.Hours) * 60 + timeSpan.Minutes) * 60 + timeSpan.Seconds;
return rtn;
}
public static int GetTimeSpan(this DateTime datevalue1, DateTime datevalue2, string spanType)
{
int rtn = 0;
switch (spanType.ToLower())
{
case "days":
rtn = GetTimeSpanDays(datevalue1, datevalue2);
break;
case "hours":
rtn = GetTimeSpanHours(datevalue1, datevalue2);
break;
case "minutes":
rtn = GetTimeSpanMinutes(datevalue1, datevalue2);
break;
case "seconds":
rtn = GetTimeSpanSeconds(datevalue1, datevalue2);
break;
default:
break;
}
return rtn;
}
}
}