Posting Date

Hi All,

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.

Any help much appreciated. Thanks.

Regards,
Lakshman Ganta.

1 Like

Last working day in a month -

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)
}
4 Likes

Thanks @KarthikByggari for your response and will try your logic and get back to you.

2 Likes

Hi All,

How to find first 3 working days in a month ?

Hi @lakshman

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());
    }

Sample Output:

image

Regards,
Karthik Byggari

4 Likes

Thanks Karthik for quick reply.

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.