Вы находитесь на странице: 1из 24

Snippet: List of Date Calculations for custom date picker (VB.

NET)
Title: List of Date Calculations for custom date picker Language:
VB.NET
Description: These classes are for generation list to be used in various date
calculations such as with a date picker that has a start date and an end date.
Views: 681
Author: Paul Moss Date Added: 9/17/2010
Copy Code
1Namespace YourCurrentNamespace
2 Public Class DateSelector
3 Public Property StartDate As Date
4 Public Property EndDate As Date
5 Public Property Name As String
6 Public Sub New()
7 End Sub
8 Public Sub New(ByVal Name As String, ByVal StartDate As Date, ByVal End
Date As Date)
9 Me.StartDate = StartDate
10 Me.EndDate = EndDate
11 Me.Name = Name
12 End Sub
13 End Class
14
15
16 Public Class DateSelectorList
17 Inherits List(Of DateSelector)
18 Public Property FirstDayOfWeek As DayOfWeek = System.DayOfWeek.Monday
19 Public Property CurrentDate As Date = Now
20
21 Public Sub New()
22 AddDefaults()
23 End Sub
24 Public Sub New(ByVal CurrentDate As Date, ByVal FirstDayOfWeek As DayO
fWeek)
25 Me.FirstDayOfWeek = FirstDayOfWeek
26 Me.CurrentDate = CurrentDate
27 AddDefaults()
28 End Sub
29
30 Private Sub AddDefaults()
31 Me.Add("Custom", Date.MinValue)
32 Me.Add("Today", CurrentDate.Date)
33 Me.Add("Yesterday", DateFunctions.Yesterday(CurrentDate))
34 Me.Add("This Week", DateFunctions.GetStartDayofWeek(CurrentDate, M
e.FirstDayOfWeek), DateFunctions.GetEndDayofWeek(CurrentDate, Me.FirstDayOfWeek)
)
35 Me.Add("Last Week", DateFunctions.GetStartDayofWeek(CurrentDate.Ad
dDays(-7), Me.FirstDayOfWeek), DateFunctions.GetEndDayofWeek(CurrentDate.AddDays
(-7), Me.FirstDayOfWeek))
36 Me.Add("This Week to Date", DateFunctions.GetStartDayofWeek(Curren
tDate, Me.FirstDayOfWeek), CurrentDate)
37 Me.Add("This Month", DateFunctions.ThisMonthStartDate(CurrentDate)
, DateFunctions.ThisMonthEndDate(CurrentDate))
38 Me.Add("This Month to Date", DateFunctions.ThisMonthStartDate(Curr
entDate), CurrentDate)
39 Me.Add("Last Month", DateFunctions.StartOfLastMonth(CurrentDate),
DateFunctions.EndOfLastMonth(CurrentDate))
40 Me.Add("Last Month to Date", DateFunctions.StartOfLastMonth(Curren
tDate), CurrentDate)
41 Me.Add("This Quarter", DateFunctions.ThisQuarterStart(CurrentDate)
, DateFunctions.ThisQuaterEnd(CurrentDate))
42 Me.Add("This Quarter to Date", DateFunctions.ThisQuarterStart(Curr
entDate), CurrentDate)
43 Me.Add("Last Quarter", DateFunctions.LastQuaterStart(CurrentDate),
DateFunctions.LastQuarterEnd(CurrentDate))
44 Me.Add("Last Quarter to Date", DateFunctions.LastQuaterStart(Curre
ntDate), CurrentDate)
45 Me.Add("First Quarter", DateFunctions.FirstQuarterStart(CurrentDat
e), DateFunctions.FirstQuarterEnd(CurrentDate))
46 Me.Add("First Quarter to Date", DateFunctions.FirstQuarterStart(Cu
rrentDate), CurrentDate)
47 Me.Add("Second Quarter", DateFunctions.SecondQuarterStart(CurrentD
ate), DateFunctions.SecondQuarterEnd(CurrentDate))
48 Me.Add("Second Quarter to Date", DateFunctions.SecondQuarterStart(
CurrentDate), CurrentDate)
49 Me.Add("Third Quarter", DateFunctions.ThirdQuarterStart(CurrentDat
e), DateFunctions.ThirdQuarterEnd(CurrentDate))
50 Me.Add("Third Quarter to Date", DateFunctions.ThirdQuarterStart(Cu
rrentDate), CurrentDate)
51 Me.Add("Fourth Quarter", DateFunctions.FourthQuarterStart(CurrentD
ate), DateFunctions.FourthQuarterEnd(CurrentDate))
52 Me.Add("Fourth Quarter to Date", DateFunctions.FourthQuarterStart(
CurrentDate), CurrentDate)
53 Me.Add("This Year", DateFunctions.ThisYearStart(CurrentDate), Date
Functions.ThisYearEnd(CurrentDate))
54 Me.Add("This Year to Date", DateFunctions.ThisYearStart(CurrentDat
e), CurrentDate)
55 Me.Add("Last Year", DateFunctions.LastYearStart(CurrentDate), Date
Functions.LastYearEnd(CurrentDate))
56 Me.Add("Last Year to Date", DateFunctions.LastYearStart(CurrentDat
e), CurrentDate)
57
58 End Sub
59
60 Public Overloads Sub Add(ByVal Name As String, ByVal StartDate As Date
)
61 Dim Ds As New DateSelector(Name, StartDate, StartDate)
62 Me.Add(Ds)
63 End Sub
64
65 Public Overloads Sub Add(ByVal Name As String, ByVal StartDate As Date
, ByVal EndDate As Date)
66 Dim Ds As New DateSelector(Name, StartDate, EndDate)
67 Me.Add(Ds)
68 End Sub
69 End Class
70End Namespace
71
Notes
Requires: http://www.codekeep.net/VBNET/code/snippets/18835/Mega-Date-Functions-
and-date-Calculations/view.aspx The DateSelectorList could be used to fill a com
bobox that then when selected would change the start date and the end date of yo
u date picker for easy date selection.
*****************
Snippet: Mega Date Functions and date Calculations (VB.NET)
Title: Mega Date Functions and date Calculations Language: VB.NET
Description: Various date functions and date calculations that aid in simple
things such as finding quarters month end and month start etc. Views: 490
Author: Paul Moss Date Added: 9/17/2010
Copy Code
1Namespace YourCurrentNamespace
2 Public Class DateFunctions
3 ''' <summary>
4 ''' Get the day of the week from the passed in date offset by what day
start the week and the number of days to move the date foward
5 ''' </summary>
6 ''' <param name="d">The Date to start from to get the day of week name<
/param>
7 ''' <param name="StartDay">The day of the week that is considered to be
the frist day of theeek</param>
8 ''' <param name="DaysOffset">The number of days to offset to get the da
y of the week</param>
9 ''' <returns></returns>
10 ''' <remarks></remarks>
11 Public Shared Function GetDayOfWeekName(ByVal d As Date, ByVal StartDa
y As DayOfWeek, ByVal DaysOffset As Integer) As String
12 Dim Dow As DayOfWeek
13 Dow = GetDayOfWeek(d, StartDay, DaysOffset)
14 Dim Result As String
15 Select Case Dow
16 Case DayOfWeek.Monday
17 Result = "Monday"
18 Case DayOfWeek.Tuesday
19 Result = "Tuesday"
20 Case DayOfWeek.Wednesday
21 Result = "Wednesday"
22 Case DayOfWeek.Thursday
23 Result = "Thursday"
24 Case DayOfWeek.Friday
25 Result = "Friday"
26 Case DayOfWeek.Saturday
27 Result = "Saturday"
28 Case DayOfWeek.Sunday
29 Result = "Sunday"
30 Case Else
31 Result = "Unknow Day"
32 End Select
33
34 Return Result
35
36 End Function
37
38
39
40 ''' <summary>
41 ''' Gets the day of the week from passed in date offset by the <paramr
ef name="DaysOffset"/> using the <paramref name="StartDay"/>
42 ''' as the start day of the week
43 ''' </summary>
44 ''' <param name="d">the date to offset from</param>
45 ''' <param name="StartDay">The Start day of the week</param>
46 ''' <param name="DaysOffset">the number of days to move to calculate t
he day of week</param>
47 ''' <returns></returns>
48 ''' <remarks>
49 ''' Whenever <paramref name="DaysOffset"/> is zero then what ever valu
e of <paramref name="StartDay"/> is will be returned
50 ''' </remarks>
51 Public Shared Function GetDayOfWeek(ByVal d As Date, ByVal StartDay As
DayOfWeek, ByVal DaysOffset As Integer) As DayOfWeek
52
53 Dim RealStartDay As Date
54
55 Dim RealDaysOffset As Integer = DaysOffset Mod 7
56
57 ' Get the difference from the d's DayofWeek and StartDayof week
58 Dim Daydiff As Integer = (StartDay - d.DayOfWeek)
59
60 ' move the date to the start day of week
61 RealStartDay = d.AddDays(Daydiff)
62
63 ' Add the days offset to the date
64 Dim NewDay As Date = RealStartDay.AddDays(RealDaysOffset)
65
66 Return NewDay.DayOfWeek
67
68
69 End Function
70
71 ''' <summary>
72 ''' Get the previous date from the current day that matches the <param
ref name="StartDay"/>
73 ''' If the passed in date matches the StartDay then the passed in date
is returned
74 ''' </summary>
75 ''' <param name="d">The date to start looking for the Start day of wee
k from</param>
76 ''' <param name="StartDay">The Day of the week to look for the date fr
om</param>
77 ''' <returns></returns>
78 ''' <remarks></remarks>
79 Public Shared Function GetStartDayofWeek(ByVal d As Date, ByVal StartD
ay As DayOfWeek) As Date
80 Dim CurrentDayOfWeek As DayOfWeek = d.DayOfWeek
81 Dim Retval As Date = d
82 While CurrentDayOfWeek <> StartDay
83 Retval = Retval.AddDays(-1)
84 CurrentDayOfWeek = Retval.DayOfWeek
85 End While
86 Return Retval
87 End Function
88
89 ''' <summary>
90 ''' Get the next date from the current day that matches the <paramref
name="StartDay"/> Plus Seven Days
91 ''' </summary>
92 ''' <param name="d">The date to start looking for the End day of week
from</param>
93 ''' <param name="StartDay">The Day of the week to look for the date fr
om</param>
94 ''' <returns></returns>
95 ''' <remarks></remarks>
96 Public Shared Function GetEndDayofWeek(ByVal d As Date, ByVal StartDay
As DayOfWeek) As Date
97 Dim CurrentDayOfWeek As DayOfWeek = d.DayOfWeek
98 Dim Retval As Date = d
99 While CurrentDayOfWeek <> StartDay
100 Retval = Retval.AddDays(-1)
101 CurrentDayOfWeek = Retval.DayOfWeek
102 End While
103 Return Retval.AddDays(6)
104 End Function
105
106 ''' <summary>
107 ''' Gets the number of days that the date is from the Start day of th
e week passed in
108 ''' </summary>
109 ''' <param name="d">The Date to start from</param>
110 ''' <param name="StartDay">The Day of the week to calculate the offse
t from</param>
111 ''' <returns>Integer representing the number of days between the pass
ed in date and the Start day of the week passed in.</returns>
112 ''' <remarks></remarks>
113 Public Shared Function GetStartDayofWeekOffset(ByVal d As Date, ByVal
StartDay As DayOfWeek) As Integer
114 Dim CurrentDate As Date = d
115 Dim StartDate As Date = GetStartDayofWeek(d, StartDay)
116 Dim Ts As TimeSpan = CurrentDate - StartDate
117 Return Ts.Days
118 End Function
119
120 ''' <summary>
121 ''' Checks to see if a date is more than the amount of <paramref name
="DaysOld"/> Passed in
122 ''' against the current date
123 ''' </summary>
124 ''' <param name="StartDate">The Date to start from</param>
125 ''' <param name="DaysOld">The number of days to check for</param>
126 ''' <returns></returns>
127 ''' <remarks></remarks>
128 Public Overloads Shared Function IsInTimeSpan(ByVal StartDate As Date
, ByVal DaysOld As Integer) As Boolean
129
130 Return IsInTimeSpan(StartDate, Now, DaysOld)
131 End Function
132
133 ''' <summary>
134 ''' Checks to see if a date is more than the amount of <paramref name
="DaysOld"/> Passed in
135 ''' against the current date
136 ''' </summary>
137 ''' <param name="StartDate">The Start Date to start from</param>
138 ''' <param name="EndDate">The End Date to check against</param>
139 ''' <param name="DaysOld">The number of days to check for</param>
140 Public Overloads Shared Function IsInTimeSpan(ByVal StartDate As Date
, ByVal EndDate As Date, ByVal DaysOld As Integer) As Boolean
141 Dim interval As TimeSpan = EndDate - StartDate
142 Return interval.Days <= DaysOld
143 End Function
144
145
146 ''' <summary>
147 ''' Get the start date of the Previous year from the Date passed in
148 ''' </summary>
149 ''' <param name="D">The Date to Calculate From</param>
150 Public Shared Function LastYearStart(ByVal D As Date) As Date
151 Return New Date(D.Year - 1, 1, 1)
152 End Function
153
154 ''' <summary>
155 ''' Get the End of the Previous Year from the Date passed in
156 ''' </summary>
157 ''' <param name="D">The Date to Calculate From</param>
158 Public Shared Function LastYearEnd(ByVal D As Date) As Date
159 Return New Date(D.Year - 1, 12, 31)
160 End Function
161
162 ''' <summary>
163 ''' Get Start of the Year from the Date passed in
164 ''' </summary>
165 ''' <param name="D">The Date to Calculate From</param>
166 Public Shared Function ThisYearStart(ByVal d As Date) As Date
167 Return New Date(d.Year, 1, 1)
168 End Function
169
170 ''' <summary>
171 ''' Get End of the Year from the Date passed in
172 ''' </summary>
173 ''' <param name="D">The Date to Calculate From</param>
174 Public Shared Function ThisYearEnd(ByVal D As Date) As Date
175 Return New Date(D.Year, 12, 31)
176 End Function
177
178 ''' <summary>
179 ''' Get the Date one year previous from the Date passed in
180 ''' </summary>
181 ''' <param name="D">The Date to Calculate From</param>
182 Public Shared Function OneYearAgo(ByVal D As Date) As Date
183 Return D.AddYears(-1)
184 End Function
185
186 ''' <summary>
187 ''' Get the Start of the Quarter from the Date passed in
188 ''' </summary>
189 ''' <param name="D">The Date to Calculate From</param>
190 Public Shared Function ThisQuarterStart(ByVal D As Date) As Date
191 Return New Date(D.Year, QuaterStartMonth(D), 1)
192 End Function
193
194 ''' <summary>
195 ''' Get the end of the Quarter from the Date passed in
196 ''' </summary>
197 ''' <param name="D">The Date to Calculate From</param>
198 Public Shared Function ThisQuaterEnd(ByVal D As Date) As Date
199 Dim M As Integer = QuaterEndMonth(D)
200 Return New Date(D.Year, M, Date.DaysInMonth(D.Year, M))
201 End Function
202
203 ''' <summary>
204 ''' Get the Start of the Previous Quarter from the Date passed in
205 ''' </summary>
206 ''' <param name="D">The Date to Calculate From</param>
207 Public Shared Function LastQuaterStart(ByVal D As Date) As Date
208 Dim M As Integer = QuaterStartMonth(D.AddMonths(-3))
209 Return New Date(D.Year, M, 1)
210 End Function
211
212 ''' <summary>
213 ''' Get the End of the Previous Quarter from the Date passed in
214 ''' </summary>
215 ''' <param name="D">The Date to Calculate From</param>
216 Public Shared Function LastQuarterEnd(ByVal D As Date) As Date
217 Dim M As Integer = QuaterEndMonth(D.AddMonths(-3))
218 Return New Date(D.Year, M, Date.DaysInMonth(D.Year, M))
219 End Function
220
221 ''' <summary>
222 ''' Get the Start of the next Quarter from the Date passed in
223 ''' </summary>
224 ''' <param name="D">The Date to Calculate From</param>
225 Public Shared Function NextQuarterStart(ByVal D As Date) As Date
226 Dim M As Integer = QuaterStartMonth(D.AddMonths(3))
227 Return New Date(D.Year, M, Date.DaysInMonth(D.Year, M))
228 End Function
229
230 ''' <summary>
231 ''' Get the End of the next Quarter from the Date passed in
232 ''' </summary>
233 ''' <param name="D">The Date to Calculate From</param>
234 Public Shared Function NextQuarterEnd(ByVal D As Date) As Date
235 Dim M As Integer = QuaterEndMonth(D.AddMonths(3))
236 Return New Date(D.Year, M, Date.DaysInMonth(D.Year, M))
237 End Function
238
239 ''' <summary>
240 ''' Get the Start of the first Quarter from the Date passed in
241 ''' </summary>
242 ''' <param name="D">The Date to Calculate From</param>
243 Public Shared Function FirstQuarterStart(ByVal D As Date) As Date
244 Return New Date(D.Year, 1, 1)
245 End Function
246
247 ''' <summary>
248 ''' Get the Start of the first Quarter from the Date passed in
249 ''' </summary>
250 ''' <param name="D">The Date to Calculate From</param>
251 Public Shared Function FirstQuarterEnd(ByVal D As Date) As Date
252 Return New Date(D.Year, 3, 31)
253 End Function
254
255 ''' <summary>
256 ''' Get the Start of the second Quarter from the Date passed in
257 ''' </summary>
258 ''' <param name="D">The Date to Calculate From</param>
259 Public Shared Function SecondQuarterStart(ByVal D As Date) As Date
260 Return New Date(D.Year, 4, 1)
261 End Function
262
263 ''' <summary>
264 ''' Get the end of the second Quarter from the Date passed in
265 ''' </summary>
266 ''' <param name="D">The Date to Calculate From</param>
267 Public Shared Function SecondQuarterEnd(ByVal D As Date) As Date
268 Return New Date(D.Year, 6, 30)
269 End Function
270
271 ''' <summary>
272 ''' Get the Start of the third Quarter from the Date passed in
273 ''' </summary>
274 ''' <param name="D">The Date to Calculate From</param>
275 Public Shared Function ThirdQuarterStart(ByVal D As Date) As Date
276 Return New Date(D.Year, 7, 1)
277 End Function
278
279 ''' <summary>
280 ''' Get the End of the third Quarter from the Date passed in
281 ''' </summary>
282 ''' <param name="D">The Date to Calculate From</param>
283 Public Shared Function ThirdQuarterEnd(ByVal D As Date) As Date
284 Return New Date(D.Year, 9, 30)
285 End Function
286
287 ''' <summary>
288 ''' Get the Start of the Forth Quarter from the Date passed in
289 ''' </summary>
290 ''' <param name="D">The Date to Calculate From</param>
291 Public Shared Function FourthQuarterStart(ByVal D As Date) As Date
292 Return New Date(D.Year, 10, 1)
293 End Function
294
295 ''' <summary>
296 ''' Get the End of the Forth Quarter from the Date passed in
297 ''' </summary>
298 ''' <param name="D">The Date to Calculate From</param>
299 Public Shared Function FourthQuarterEnd(ByVal D As Date) As Date
300 Return New Date(D.Year, 12, 31)
301 End Function
302
303 ''' <summary>
304 ''' Get the start of the previous month from the Date passed in
305 ''' </summary>
306 ''' <param name="D">The Date to Calculate From</param>
307 Public Shared Function StartOfLastMonth(ByVal D As Date) As Date
308 Dim NewDate As Date = D.AddMonths(-1)
309 Return New Date(NewDate.Year, NewDate.Month, 1)
310 End Function
311
312 ''' <summary>
313 ''' Get the end of the previous month from the Date passed in
314 ''' </summary>
315 ''' <param name="D">The Date to Calculate From</param>
316 Public Shared Function EndOfLastMonth(ByVal D As Date) As Date
317 Dim NewDate As Date = D.AddMonths(-1)
318 Dim Days As Integer = Date.DaysInMonth(NewDate.Year, NewDate.Mont
h)
319 Return New Date(NewDate.Year, NewDate.Month, Days)
320 End Function
321
322 ''' <summary>
323 ''' Get the start of the next month from the Date passed in
324 ''' </summary>
325 ''' <param name="D">The Date to Calculate From</param>
326 Public Shared Function StartOfNextMonth(ByVal D As Date) As Date
327 Dim NewDate As Date = D.AddMonths(1)
328 Return New Date(NewDate.Year, 1, 1)
329 End Function
330
331 ''' <summary>
332 ''' Get the end of the next month from the Date passed in
333 ''' </summary>
334 ''' <param name="D">The Date to Calculate From</param>
335 Public Shared Function EndOfNextMonth(ByVal D As Date) As Date
336 Dim NewDate As Date = D.AddMonths(1)
337 Dim Days As Integer = Date.DaysInMonth(NewDate.Year, NewDate.Mont
h)
338 Return New Date(NewDate.Year, NewDate.Month, Days)
339 End Function
340
341 ''' <summary>
342 ''' Get the date one day previous from the Date passed in
343 ''' </summary>
344 ''' <param name="D">The Date to Calculate From</param>
345 Public Shared Function Yesterday(ByVal D As Date) As Date
346 Return D.AddDays(-1)
347 End Function
348
349 ''' <summary>
350 ''' Get the date for the next date from the Date passed in
351 ''' </summary>
352 ''' <param name="D">The Date to Calculate From</param>
353 Public Shared Function Tomorrow(ByVal D As Date) As Date
354 Return D.AddDays(1)
355 End Function
356
357 ''' <summary>
358 ''' Get the start of the month from the Date passed in
359 ''' </summary>
360 ''' <param name="D">The Date to Calculate From</param>
361 Public Shared Function ThisMonthStartDate(ByVal D As Date) As Date
362 Return New Date(D.Year, D.Month, 1)
363 End Function
364
365 ''' <summary>
366 ''' Get the end of the month from the Date passed in
367 ''' </summary>
368 ''' <param name="D">The Date to Calculate From</param>
369 Public Shared Function ThisMonthEndDate(ByVal D As Date) As Date
370 Dim Days As Integer = Date.DaysInMonth(D.Year, D.Month)
371 Return New Date(D.Year, D.Month, Days)
372 End Function
373
374 ''' <summary>
375 ''' Get the Start of the next month from the Date passed in
376 ''' </summary>
377 ''' <param name="D">The Date to Calculate From</param>
378 Public Shared Function NextMonthStartDate(ByVal D As Date) As Date
379 Return ThisMonthStartDate(D.AddMonths(1))
380 End Function
381
382 ''' <summary>
383 ''' Get the end of the next month from the Date passed in
384 ''' </summary>
385 ''' <param name="D">The Date to Calculate From</param>
386 Public Shared Function NextMonthEndDate(ByVal D As Date) As Date
387 Return ThisMonthEndDate(D.AddMonths(1))
388 End Function
389
390 ''' <summary>
391 ''' Get the Quarter from the Date passed in
392 ''' </summary>
393 ''' <param name="D">The Date to Calculate From</param>
394 Public Shared Function Quater(ByVal D As Date) As Integer
395 Dim retval As Integer
396 Select Case True
397 Case D.Month > 9
398 retval = 4
399 Exit Select
400 Case D.Month > 6
401 retval = 3
402 Exit Select
403 Case D.Month > 3
404 retval = 2
405 Exit Select
406 Case Else
407 retval = 1
408 Exit Select
409 End Select
410 Return retval
411 End Function
412
413 ''' <summary>
414 ''' Get the start of the Quarter from the Date passed in
415 ''' </summary>
416 ''' <param name="D">The Date to Calculate From</param>
417 Public Shared Function QuaterStartMonth(ByVal D As Date) As Integer
418 Dim Q As Integer = Quater(D)
419 Select Case Q
420 Case 1
421 Return 1
422 Case 2
423 Return 4
424 Case 3
425 Return 7
426 Case Else
427 Return 10
428 End Select
429 End Function
430
431 ''' <summary>
432 ''' Get the end of the Quarter from the Date passed in
433 ''' </summary>
434 ''' <param name="D">The Date to Calculate From</param>
435 Public Shared Function QuaterEndMonth(ByVal D As Date) As Integer
436 Dim Q As Integer = Quater(D)
437 Select Case Q
438 Case 1
439 Return 3
440 Case 2
441 Return 6
442 Case 3
443 Return 9
444 Case Else
445 Return 12
446 End Select
447 End Function
448
449 End Class
450End Namespace
451
452
Notes
Also see: http://www.codekeep.net/snippets/6ab4455b-f582-492c-88c1-34a330d4080b.
aspx Enjoy!
********************
Snippet: List of Date Calculations for custom date picker (VB.NET)
Title: List of Date Calculations for custom date picker Language:
VB.NET
Description: These classes are for generation list to be used in various date
calculations such as with a date picker that has a start date and an end date.
Views: 682
Author: Paul Moss Date Added: 9/17/2010
Copy Code
1Namespace YourCurrentNamespace
2 Public Class DateSelector
3 Public Property StartDate As Date
4 Public Property EndDate As Date
5 Public Property Name As String
6 Public Sub New()
7 End Sub
8 Public Sub New(ByVal Name As String, ByVal StartDate As Date, ByVal End
Date As Date)
9 Me.StartDate = StartDate
10 Me.EndDate = EndDate
11 Me.Name = Name
12 End Sub
13 End Class
14
15
16 Public Class DateSelectorList
17 Inherits List(Of DateSelector)
18 Public Property FirstDayOfWeek As DayOfWeek = System.DayOfWeek.Monday
19 Public Property CurrentDate As Date = Now
20
21 Public Sub New()
22 AddDefaults()
23 End Sub
24 Public Sub New(ByVal CurrentDate As Date, ByVal FirstDayOfWeek As DayO
fWeek)
25 Me.FirstDayOfWeek = FirstDayOfWeek
26 Me.CurrentDate = CurrentDate
27 AddDefaults()
28 End Sub
29
30 Private Sub AddDefaults()
31 Me.Add("Custom", Date.MinValue)
32 Me.Add("Today", CurrentDate.Date)
33 Me.Add("Yesterday", DateFunctions.Yesterday(CurrentDate))
34 Me.Add("This Week", DateFunctions.GetStartDayofWeek(CurrentDate, M
e.FirstDayOfWeek), DateFunctions.GetEndDayofWeek(CurrentDate, Me.FirstDayOfWeek)
)
35 Me.Add("Last Week", DateFunctions.GetStartDayofWeek(CurrentDate.Ad
dDays(-7), Me.FirstDayOfWeek), DateFunctions.GetEndDayofWeek(CurrentDate.AddDays
(-7), Me.FirstDayOfWeek))
36 Me.Add("This Week to Date", DateFunctions.GetStartDayofWeek(Curren
tDate, Me.FirstDayOfWeek), CurrentDate)
37 Me.Add("This Month", DateFunctions.ThisMonthStartDate(CurrentDate)
, DateFunctions.ThisMonthEndDate(CurrentDate))
38 Me.Add("This Month to Date", DateFunctions.ThisMonthStartDate(Curr
entDate), CurrentDate)
39 Me.Add("Last Month", DateFunctions.StartOfLastMonth(CurrentDate),
DateFunctions.EndOfLastMonth(CurrentDate))
40 Me.Add("Last Month to Date", DateFunctions.StartOfLastMonth(Curren
tDate), CurrentDate)
41 Me.Add("This Quarter", DateFunctions.ThisQuarterStart(CurrentDate)
, DateFunctions.ThisQuaterEnd(CurrentDate))
42 Me.Add("This Quarter to Date", DateFunctions.ThisQuarterStart(Curr
entDate), CurrentDate)
43 Me.Add("Last Quarter", DateFunctions.LastQuaterStart(CurrentDate),
DateFunctions.LastQuarterEnd(CurrentDate))
44 Me.Add("Last Quarter to Date", DateFunctions.LastQuaterStart(Curre
ntDate), CurrentDate)
45 Me.Add("First Quarter", DateFunctions.FirstQuarterStart(CurrentDat
e), DateFunctions.FirstQuarterEnd(CurrentDate))
46 Me.Add("First Quarter to Date", DateFunctions.FirstQuarterStart(Cu
rrentDate), CurrentDate)
47 Me.Add("Second Quarter", DateFunctions.SecondQuarterStart(CurrentD
ate), DateFunctions.SecondQuarterEnd(CurrentDate))
48 Me.Add("Second Quarter to Date", DateFunctions.SecondQuarterStart(
CurrentDate), CurrentDate)
49 Me.Add("Third Quarter", DateFunctions.ThirdQuarterStart(CurrentDat
e), DateFunctions.ThirdQuarterEnd(CurrentDate))
50 Me.Add("Third Quarter to Date", DateFunctions.ThirdQuarterStart(Cu
rrentDate), CurrentDate)
51 Me.Add("Fourth Quarter", DateFunctions.FourthQuarterStart(CurrentD
ate), DateFunctions.FourthQuarterEnd(CurrentDate))
52 Me.Add("Fourth Quarter to Date", DateFunctions.FourthQuarterStart(
CurrentDate), CurrentDate)
53 Me.Add("This Year", DateFunctions.ThisYearStart(CurrentDate), Date
Functions.ThisYearEnd(CurrentDate))
54 Me.Add("This Year to Date", DateFunctions.ThisYearStart(CurrentDat
e), CurrentDate)
55 Me.Add("Last Year", DateFunctions.LastYearStart(CurrentDate), Date
Functions.LastYearEnd(CurrentDate))
56 Me.Add("Last Year to Date", DateFunctions.LastYearStart(CurrentDat
e), CurrentDate)
57
58 End Sub
59
60 Public Overloads Sub Add(ByVal Name As String, ByVal StartDate As Date
)
61 Dim Ds As New DateSelector(Name, StartDate, StartDate)
62 Me.Add(Ds)
63 End Sub
64
65 Public Overloads Sub Add(ByVal Name As String, ByVal StartDate As Date
, ByVal EndDate As Date)
66 Dim Ds As New DateSelector(Name, StartDate, EndDate)
67 Me.Add(Ds)
68 End Sub
69 End Class
70End Namespace
71
Notes
Requires: http://www.codekeep.net/VBNET/code/snippets/18835/Mega-Date-Functions-
and-date-Calculations/view.aspx The DateSelectorList could be used to fill a com
bobox that then when selected would change the start date and the end date of yo
u date picker for easy date selection.
**********************************
Snippet: Mega Date Functions and date Calculations (VB.NET)
Title: Mega Date Functions and date Calculations Language: VB.NET
Description: Various date functions and date calculations that aid in simple
things such as finding quarters month end and month start etc. Views: 491
Author: Paul Moss Date Added: 9/17/2010
Namespace YourCurrentNamespace
Public Class DateFunctions
''' <summary>
''' Get the day of the week from the passed in date offset by what day s
tart the week and the number of days to move the date foward
''' </summary>
''' <param name="d">The Date to start from to get the day of week name</
param>
''' <param name="StartDay">The day of the week that is considered to be
the frist day of theeek</param>
''' <param name="DaysOffset">The number of days to offset to get the day
of the week</param>
''' <returns></returns>
''' <remarks></remarks>
Public Shared Function GetDayOfWeekName(ByVal d As Date, ByVal StartDay
As DayOfWeek, ByVal DaysOffset As Integer) As String
Dim Dow As DayOfWeek
Dow = GetDayOfWeek(d, StartDay, DaysOffset)
Dim Result As String
Select Case Dow
Case DayOfWeek.Monday
Result = "Monday"
Case DayOfWeek.Tuesday
Result = "Tuesday"
Case DayOfWeek.Wednesday
Result = "Wednesday"
Case DayOfWeek.Thursday
Result = "Thursday"
Case DayOfWeek.Friday
Result = "Friday"
Case DayOfWeek.Saturday
Result = "Saturday"
Case DayOfWeek.Sunday
Result = "Sunday"
Case Else
Result = "Unknow Day"
End Select
Return Result
End Function
''' <summary>
''' Gets the day of the week from passed in date offset by the <paramref
name="DaysOffset"/> using the <paramref name="StartDay"/>
''' as the start day of the week
''' </summary>
''' <param name="d">the date to offset from</param>
''' <param name="StartDay">The Start day of the week</param>
''' <param name="DaysOffset">the number of days to move to calculate the
day of week</param>
''' <returns></returns>
''' <remarks>
''' Whenever <paramref name="DaysOffset"/> is zero then what ever value
of <paramref name="StartDay"/> is will be returned
''' </remarks>
Public Shared Function GetDayOfWeek(ByVal d As Date, ByVal StartDay As D
ayOfWeek, ByVal DaysOffset As Integer) As DayOfWeek
Dim RealStartDay As Date
Dim RealDaysOffset As Integer = DaysOffset Mod 7
' Get the difference from the d's DayofWeek and StartDayof week
Dim Daydiff As Integer = (StartDay - d.DayOfWeek)
' move the date to the start day of week
RealStartDay = d.AddDays(Daydiff)
' Add the days offset to the date
Dim NewDay As Date = RealStartDay.AddDays(RealDaysOffset)
Return NewDay.DayOfWeek
End Function
''' <summary>
''' Get the previous date from the current day that matches the <paramre
f name="StartDay"/>
''' If the passed in date matches the StartDay then the passed in date i
s returned
''' </summary>
''' <param name="d">The date to start looking for the Start day of week
from</param>
''' <param name="StartDay">The Day of the week to look for the date from
</param>
''' <returns></returns>
''' <remarks></remarks>
Public Shared Function GetStartDayofWeek(ByVal d As Date, ByVal StartDay
As DayOfWeek) As Date
Dim CurrentDayOfWeek As DayOfWeek = d.DayOfWeek
Dim Retval As Date = d
While CurrentDayOfWeek <> StartDay
Retval = Retval.AddDays(-1)
CurrentDayOfWeek = Retval.DayOfWeek
End While
Return Retval
End Function
''' <summary>
''' Get the next date from the current day that matches the <paramref na
me="StartDay"/> Plus Seven Days
''' </summary>
''' <param name="d">The date to start looking for the End day of week fr
om</param>
''' <param name="StartDay">The Day of the week to look for the date from
</param>
''' <returns></returns>
''' <remarks></remarks>
Public Shared Function GetEndDayofWeek(ByVal d As Date, ByVal StartDay A
s DayOfWeek) As Date
Dim CurrentDayOfWeek As DayOfWeek = d.DayOfWeek
Dim Retval As Date = d
While CurrentDayOfWeek <> StartDay
Retval = Retval.AddDays(-1)
CurrentDayOfWeek = Retval.DayOfWeek
End While
Return Retval.AddDays(6)
End Function
''' <summary>
''' Gets the number of days that the date is from the Start day of the w
eek passed in
''' </summary>
''' <param name="d">The Date to start from</param>
''' <param name="StartDay">The Day of the week to calculate the offset f
rom</param>
''' <returns>Integer representing the number of days between the passed
in date and the Start day of the week passed in.</returns>
''' <remarks></remarks>
Public Shared Function GetStartDayofWeekOffset(ByVal d As Date, ByVal St
artDay As DayOfWeek) As Integer
Dim CurrentDate As Date = d
Dim StartDate As Date = GetStartDayofWeek(d, StartDay)
Dim Ts As TimeSpan = CurrentDate - StartDate
Return Ts.Days
End Function
''' <summary>
''' Checks to see if a date is more than the amount of <paramref name="D
aysOld"/> Passed in
''' against the current date
''' </summary>
''' <param name="StartDate">The Date to start from</param>
''' <param name="DaysOld">The number of days to check for</param>
''' <returns></returns>
''' <remarks></remarks>
Public Overloads Shared Function IsInTimeSpan(ByVal StartDate As Date, B
yVal DaysOld As Integer) As Boolean
Return IsInTimeSpan(StartDate, Now, DaysOld)
End Function
''' <summary>
''' Checks to see if a date is more than the amount of <paramref name="D
aysOld"/> Passed in
''' against the current date
''' </summary>
''' <param name="StartDate">The Start Date to start from</param>
''' <param name="EndDate">The End Date to check against</param>
''' <param name="DaysOld">The number of days to check for</param>
Public Overloads Shared Function IsInTimeSpan(ByVal StartDate As Date, B
yVal EndDate As Date, ByVal DaysOld As Integer) As Boolean
Dim interval As TimeSpan = EndDate - StartDate
Return interval.Days <= DaysOld
End Function
''' <summary>
''' Get the start date of the Previous year from the Date passed in
''' </summary>
''' <param name="D">The Date to Calculate From</param>
Public Shared Function LastYearStart(ByVal D As Date) As Date
Return New Date(D.Year - 1, 1, 1)
End Function
''' <summary>
''' Get the End of the Previous Year from the Date passed in
''' </summary>
''' <param name="D">The Date to Calculate From</param>
Public Shared Function LastYearEnd(ByVal D As Date) As Date
Return New Date(D.Year - 1, 12, 31)
End Function
''' <summary>
''' Get Start of the Year from the Date passed in
''' </summary>
''' <param name="D">The Date to Calculate From</param>
Public Shared Function ThisYearStart(ByVal d As Date) As Date
Return New Date(d.Year, 1, 1)
End Function
''' <summary>
''' Get End of the Year from the Date passed in
''' </summary>
''' <param name="D">The Date to Calculate From</param>
Public Shared Function ThisYearEnd(ByVal D As Date) As Date
Return New Date(D.Year, 12, 31)
End Function
''' <summary>
''' Get the Date one year previous from the Date passed in
''' </summary>
''' <param name="D">The Date to Calculate From</param>
Public Shared Function OneYearAgo(ByVal D As Date) As Date
Return D.AddYears(-1)
End Function
''' <summary>
''' Get the Start of the Quarter from the Date passed in
''' </summary>
''' <param name="D">The Date to Calculate From</param>
Public Shared Function ThisQuarterStart(ByVal D As Date) As Date
Return New Date(D.Year, QuaterStartMonth(D), 1)
End Function
''' <summary>
''' Get the end of the Quarter from the Date passed in
''' </summary>
''' <param name="D">The Date to Calculate From</param>
Public Shared Function ThisQuaterEnd(ByVal D As Date) As Date
Dim M As Integer = QuaterEndMonth(D)
Return New Date(D.Year, M, Date.DaysInMonth(D.Year, M))
End Function
''' <summary>
''' Get the Start of the Previous Quarter from the Date passed in
''' </summary>
''' <param name="D">The Date to Calculate From</param>
Public Shared Function LastQuaterStart(ByVal D As Date) As Date
Dim M As Integer = QuaterStartMonth(D.AddMonths(-3))
Return New Date(D.Year, M, 1)
End Function
''' <summary>
''' Get the End of the Previous Quarter from the Date passed in
''' </summary>
''' <param name="D">The Date to Calculate From</param>
Public Shared Function LastQuarterEnd(ByVal D As Date) As Date
Dim M As Integer = QuaterEndMonth(D.AddMonths(-3))
Return New Date(D.Year, M, Date.DaysInMonth(D.Year, M))
End Function
''' <summary>
''' Get the Start of the next Quarter from the Date passed in
''' </summary>
''' <param name="D">The Date to Calculate From</param>
Public Shared Function NextQuarterStart(ByVal D As Date) As Date
Dim M As Integer = QuaterStartMonth(D.AddMonths(3))
Return New Date(D.Year, M, Date.DaysInMonth(D.Year, M))
End Function
''' <summary>
''' Get the End of the next Quarter from the Date passed in
''' </summary>
''' <param name="D">The Date to Calculate From</param>
Public Shared Function NextQuarterEnd(ByVal D As Date) As Date
Dim M As Integer = QuaterEndMonth(D.AddMonths(3))
Return New Date(D.Year, M, Date.DaysInMonth(D.Year, M))
End Function
''' <summary>
''' Get the Start of the first Quarter from the Date passed in
''' </summary>
''' <param name="D">The Date to Calculate From</param>
Public Shared Function FirstQuarterStart(ByVal D As Date) As Date
Return New Date(D.Year, 1, 1)
End Function
''' <summary>
''' Get the Start of the first Quarter from the Date passed in
''' </summary>
''' <param name="D">The Date to Calculate From</param>
Public Shared Function FirstQuarterEnd(ByVal D As Date) As Date
Return New Date(D.Year, 3, 31)
End Function
''' <summary>
''' Get the Start of the second Quarter from the Date passed in
''' </summary>
''' <param name="D">The Date to Calculate From</param>
Public Shared Function SecondQuarterStart(ByVal D As Date) As Date
Return New Date(D.Year, 4, 1)
End Function
''' <summary>
''' Get the end of the second Quarter from the Date passed in
''' </summary>
''' <param name="D">The Date to Calculate From</param>
Public Shared Function SecondQuarterEnd(ByVal D As Date) As Date
Return New Date(D.Year, 6, 30)
End Function
''' <summary>
''' Get the Start of the third Quarter from the Date passed in
''' </summary>
''' <param name="D">The Date to Calculate From</param>
Public Shared Function ThirdQuarterStart(ByVal D As Date) As Date
Return New Date(D.Year, 7, 1)
End Function
''' <summary>
''' Get the End of the third Quarter from the Date passed in
''' </summary>
''' <param name="D">The Date to Calculate From</param>
Public Shared Function ThirdQuarterEnd(ByVal D As Date) As Date
Return New Date(D.Year, 9, 30)
End Function
''' <summary>
''' Get the Start of the Forth Quarter from the Date passed in
''' </summary>
''' <param name="D">The Date to Calculate From</param>
Public Shared Function FourthQuarterStart(ByVal D As Date) As Date
Return New Date(D.Year, 10, 1)
End Function
''' <summary>
''' Get the End of the Forth Quarter from the Date passed in
''' </summary>
''' <param name="D">The Date to Calculate From</param>
Public Shared Function FourthQuarterEnd(ByVal D As Date) As Date
Return New Date(D.Year, 12, 31)
End Function
''' <summary>
''' Get the start of the previous month from the Date passed in
''' </summary>
''' <param name="D">The Date to Calculate From</param>
Public Shared Function StartOfLastMonth(ByVal D As Date) As Date
Dim NewDate As Date = D.AddMonths(-1)
Return New Date(NewDate.Year, NewDate.Month, 1)
End Function
''' <summary>
''' Get the end of the previous month from the Date passed in
''' </summary>
''' <param name="D">The Date to Calculate From</param>
Public Shared Function EndOfLastMonth(ByVal D As Date) As Date
Dim NewDate As Date = D.AddMonths(-1)
Dim Days As Integer = Date.DaysInMonth(NewDate.Year, NewDate.Month)
Return New Date(NewDate.Year, NewDate.Month, Days)
End Function
''' <summary>
''' Get the start of the next month from the Date passed in
''' </summary>
''' <param name="D">The Date to Calculate From</param>
Public Shared Function StartOfNextMonth(ByVal D As Date) As Date
Dim NewDate As Date = D.AddMonths(1)
Return New Date(NewDate.Year, 1, 1)
End Function
''' <summary>
''' Get the end of the next month from the Date passed in
''' </summary>
''' <param name="D">The Date to Calculate From</param>
Public Shared Function EndOfNextMonth(ByVal D As Date) As Date
Dim NewDate As Date = D.AddMonths(1)
Dim Days As Integer = Date.DaysInMonth(NewDate.Year, NewDate.Month)
Return New Date(NewDate.Year, NewDate.Month, Days)
End Function
''' <summary>
''' Get the date one day previous from the Date passed in
''' </summary>
''' <param name="D">The Date to Calculate From</param>
Public Shared Function Yesterday(ByVal D As Date) As Date
Return D.AddDays(-1)
End Function
''' <summary>
''' Get the date for the next date from the Date passed in
''' </summary>
''' <param name="D">The Date to Calculate From</param>
Public Shared Function Tomorrow(ByVal D As Date) As Date
Return D.AddDays(1)
End Function
''' <summary>
''' Get the start of the month from the Date passed in
''' </summary>
''' <param name="D">The Date to Calculate From</param>
Public Shared Function ThisMonthStartDate(ByVal D As Date) As Date
Return New Date(D.Year, D.Month, 1)
End Function
''' <summary>
''' Get the end of the month from the Date passed in
''' </summary>
''' <param name="D">The Date to Calculate From</param>
Public Shared Function ThisMonthEndDate(ByVal D As Date) As Date
Dim Days As Integer = Date.DaysInMonth(D.Year, D.Month)
Return New Date(D.Year, D.Month, Days)
End Function
''' <summary>
''' Get the Start of the next month from the Date passed in
''' </summary>
''' <param name="D">The Date to Calculate From</param>
Public Shared Function NextMonthStartDate(ByVal D As Date) As Date
Return ThisMonthStartDate(D.AddMonths(1))
End Function
''' <summary>
''' Get the end of the next month from the Date passed in
''' </summary>
''' <param name="D">The Date to Calculate From</param>
Public Shared Function NextMonthEndDate(ByVal D As Date) As Date
Return ThisMonthEndDate(D.AddMonths(1))
End Function
''' <summary>
''' Get the Quarter from the Date passed in
''' </summary>
''' <param name="D">The Date to Calculate From</param>
Public Shared Function Quater(ByVal D As Date) As Integer
Dim retval As Integer
Select Case True
Case D.Month > 9
retval = 4
Exit Select
Case D.Month > 6
retval = 3
Exit Select
Case D.Month > 3
retval = 2
Exit Select
Case Else
retval = 1
Exit Select
End Select
Return retval
End Function
''' <summary>
''' Get the start of the Quarter from the Date passed in
''' </summary>
''' <param name="D">The Date to Calculate From</param>
Public Shared Function QuaterStartMonth(ByVal D As Date) As Integer
Dim Q As Integer = Quater(D)
Select Case Q
Case 1
Return 1
Case 2
Return 4
Case 3
Return 7
Case Else
Return 10
End Select
End Function
''' <summary>
''' Get the end of the Quarter from the Date passed in
''' </summary>
''' <param name="D">The Date to Calculate From</param>
Public Shared Function QuaterEndMonth(ByVal D As Date) As Integer
Dim Q As Integer = Quater(D)
Select Case Q
Case 1
Return 3
Case 2
Return 6
Case 3
Return 9
Case Else
Return 12
End Select
End Function
End Class
End Namespace
*******************************

Вам также может понравиться