Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var holidays = new List<DateTime>{/* list of observed holidays */};
- DateTime lastBusinessDay = new DateTime();
- var i = DateTime.DaysInMonth(month, year);
- while (i > 0)
- {
- var dtCurrent = new DateTime(year, month, i);
- if(dtCurrent.DayofWeek < DayOfWeek.Saturday && dtCurrent.DayOfWeek >Sunday &&
- !holidays.Contains(dtCurrent))
- {
- lastBusinessDay = dtCurrent;
- i = 0;
- }
- else
- {
- i = i.AddDays(-1);
- }
- }
- Function GetLastBusinessDay(ByVal Year As Integer, ByVal Month As Integer) As DateTime
- Dim LastOfMonth As DateTime
- Dim LastBusinessDay As DateTime
- LastOfMonth = New DateTime(Year, Month, DateTime.DaysInMonth(Year, Month))
- If LastOfMonth.DayOfWeek = DayOfWeek.Sunday Then
- LastBusinessDay = LastOfMonth.AddDays(-2)
- ElseIf LastOfMonth.DayOfWeek = DayOfWeek.Saturday Then
- LastBusinessDay = LastOfMonth.AddDays(-1)
- Else
- LastBusinessDay = LastOfMonth
- End If
- Return LastBusinessDay
- End Function
- Day day = getLastDayOfMonth
- int days = getDaysInMonth
- for i = days to 0
- if day is weekday
- if day is not holiday
- return day
- end if
- end if
- day = prevDay
- days--
- end for
- throw exception because no business day was found in the month
- Function GetLastDay(ByVal month As Int32, ByVal year As Int32) As Date
- Dim D As New Date(year, month, Date.DaysInMonth(year, month))
- For i As Integer = 0 To Date.DaysInMonth(year, month)
- Select Case D.AddDays(-i).DayOfWeek
- Case DayOfWeek.Saturday, DayOfWeek.Sunday 'Not a weekday
- Case Else 'Is a weekday. Flag as first weekday found
- Return D.AddDays(-i)
- 'you can add other code here which could also do a check for holidays or ther stuff since you have a proper date value to look at in the loop
- End Select
- Next
- End Function
Add Comment
Please, Sign In to add comment