How can I calculate the last business day of the month in .NET?
First, get the last day of the month. Then keep decrementing until you're either past the beginning of the month, or have hit a date that validates as a "business day".
I don't believe there's a built in way, but it shouldn't be too hard to write a method to figure it out.
general purpose, pseudocode:
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
Assuming business days are monday to friday (this doesn't account for holidays), this function should return the proper answer:
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
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
I would do it like this for a Monday through Friday business week:
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);
}
}
Be sure to not forget public holidays. Those make it a little harder since there are lots of regional differences and are mostly not given by a simple formula. You may need to look them up somewhere or make a table with the dates yourself.
can u healp me to faind the pseudocod of this program: // This program outputs a calendar. // By Neil Broadbent import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.IOException; import java.util.StringTokenizer;
public class daysInMonth
{
public static void main(String[] pArgs) throws IOException
{
final BufferedReader tKeyboard = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Type in the number of days in the month (0-31)");
System.out.flush();
String tLine = tKeyboard.readLine();
int tDays = new Integer(tLine).intValue();
System.out.print("Which day of the week does the month start? (0-6)");
System.out.flush();
String tLine2 = tKeyboard.readLine();
final int tStartDay = new Integer(tLine2).intValue();
int D1 = 0;
System.out.println("S M T W Th F S" );
for (int tSpaces = 0; tSpaces<tStartDay; tSpaces++)
{
System.out.print(" ");
}
int tEndFirst = 7-tStartDay;
for (D1 = 1; D1<=tEndFirst; D1++)
{
System.out.print(D1/10);
System.out.print(D1%10 + " ");
}
int tDayNumber = D1;
for (int tRows = 0; tRows<5; tRows++)
{
System.out.println("");
for (int tColumns = 0; tColumns<7; tColumns++)
{
if (tDayNumber<=tDays)
{
System.out.print(tDayNumber/10);
System.out.print(tDayNumber%10 + " ");
tDayNumber++;
}
}
}
System.out.println();
}
}