I am reading data from bank invoice pdf file and entering into SAP application. Here, i need to enter posting date(By default, it is taking current date) and its fine for all dates except month end dates.
Could you please help me in the below logic:
If (document date = month end working date) then {posting date = document date}
Else {posting date = Current date}
Note: If current date day is saturday or sunday then we need to enter monday’s date as posting date.
DateTime lastBusinessDay = new DateTime()
int i = DateTime.DaysInMonth(year, month)
while (i > 0)
{
DateTime dtCurrent = new DateTime(year, month, i);
if(dtCurrent.DayOfWeek < DayOfWeek.Saturday && dtCurrent.DayOfWeek > DayOfWeek.Sunday)
{
lastBusinessDay = dtCurrent
i = 0
}
else
{
i = i - 1
}
For current date, you can use the following logic -
DateTime.Now.DayOfWeek - returns day name eg., Saturday, Sunday etc.,
If(DateTime.Now.DayOfWeek == DayOfWeek.Saturday or DateTime.Now.DayOfWeek == DayOfWeek.Sunday)
{
if saturday, date = DateTime.Now.AddDays(2)
else
date = DateTime.Now.AddDays(1)
}
Just take the first three values from a list to get the first three working days. The below code will create a list with all working days in a month. So you can extract how many working days you want.
public static void GetWorkingDaysInAMonth()
{
List<DateTime> workingDays = new List<DateTime>();
int i = DateTime.DaysInMonth(2019, 1);
for (int index=1; index <= i; index++)
{
DateTime dtCurrent = new DateTime(2019, 1, index);
if (dtCurrent.DayOfWeek < DayOfWeek.Saturday && dtCurrent.DayOfWeek > DayOfWeek.Sunday)
{
workingDays.Add(dtCurrent);
}
}
foreach (var item in workingDays)
Console.WriteLine(item.ToShortDateString());
}