Date Calculation

Patnaik14

Member
Hi All,

I have a requirement as below:-

New Delivery Date:19-06-2018 (Not Constant always)
In the previous stage we are getting a Day Count (Like: For Monday:1, Tuesday:2, Wednesday:3)
Let's assume the Day Count = 3 here...

So the requirement is to change the New Delivery Date to 3rd Day of Next Week (Here it should be 27-06-2018, Wednesday )

Please advice....Thanks...
 

sivagelli

Well-Known Member
Usually, the start day of the week depends on the location. This gets tricky.

The below function accepts 3 arguments - Date, Start day of the week, number you want to add.
  • First argument is of type DateTime: Pass the date you want to consider for addition. In your case, its delivery date you want to change
  • Second Argument: I assumed Start Day of the week is Monday
  • Third Argument: Number of the date. 1- Monday, 2- Tuesday, 3- Wednesday, 4- Thursday...
Paste the below C# code in the Global code section of Initialize. (Change the language option to C#)

C#:
public static DateTime GetFirstDateOfWeek(DateTime dayInWeek, DayOfWeek firstDay, int i)
{
    DateTime firstDayInWeek = dayInWeek.Date;
    while (firstDayInWeek.DayOfWeek != firstDay)
        firstDayInWeek = firstDayInWeek.AddDays(-1);

    return firstDayInWeek.AddDays(7+ (i-1));
}

Now, add a Code Stage
  • Input:
    • DeliveryDate of Type DateTime
    • AddNumber of Type Number
  • Output:
    • NewDeliveryDate of Type DateTime
  • Code: As I said, I assumed the start of week as Monday and passed as an argument. If you wish to change this to Sunday, the code above needs little tweak to get the desired results.
    • NewDeliveryDate = GetFirstDateOfWeek(DeliveryDate , DayOfWeek.Monday, AddNumber)
Passing DeliveryDate as "19/06/2018" and AddNumber as 3 would result in NewDeliveryDate as "27/06/2018"

Post back how it goes!
 

VJR

Well-Known Member
Hi Patnaik14,

Basically the Pseudo logic for it would be as below.

Logic:
- Inputdate = 19/06/2018
- Day Count = 3

-Add 7 days to go to the next week (NextWeekDate = 26/06/2018)
-Find out the weekday number of this date (WeekdayNumber = 2 if Monday is 1)
I did not find a direct function to find the weekday number and hence used the Choice stages there. You can modify the Choice stages if you would like to have Sunday as the 1st day of week.

- Calculate the final result:
- If WeekdayNumber < Day Count then Add (Day Count - WeekdayNumber) to NextWeekDate and store it in Result
- If WeekdayNumber > Day Count then Sub (WeekdayNumber - Day Count) to NextWeekDate and store it in Result
- If WeekdayNumber = Day Count then No change needed as NextWeekDate is the final desired date and store it in Result

- Result data item will contain the final answer

Key pointers:
- It is not straight forward to explain the above logic via Blue Prism here and also the diagram does not fit clearly in a single screenshot, hence I have attached the xml of the diagram
- The two exception stages are just for your understanding and you need to handle those stages as per your requirements

Refer Attached XML and import it in Process studio.

Run it against various test input scenarios and let me know what you feel about it.
 

Attachments

  • BPA Process - NextWeekDate.zip
    2.7 KB · Views: 63

Patnaik14

Member
Hi Patnaik14,

Basically the Pseudo logic for it would be as below.

Logic:
- Inputdate = 19/06/2018
- Day Count = 3

-Add 7 days to go to the next week (NextWeekDate = 26/06/2018)
-Find out the weekday number of this date (WeekdayNumber = 2 if Monday is 1)
I did not find a direct function to find the weekday number and hence used the Choice stages there. You can modify the Choice stages if you would like to have Sunday as the 1st day of week.

- Calculate the final result:
- If WeekdayNumber < Day Count then Add (Day Count - WeekdayNumber) to NextWeekDate and store it in Result
- If WeekdayNumber > Day Count then Sub (WeekdayNumber - Day Count) to NextWeekDate and store it in Result
- If WeekdayNumber = Day Count then No change needed as NextWeekDate is the final desired date and store it in Result

- Result data item will contain the final answer

Key pointers:
- It is not straight forward to explain the above logic via Blue Prism here and also the diagram does not fit clearly in a single screenshot, hence I have attached the xml of the diagram
- The two exception stages are just for your understanding and you need to handle those stages as per your requirements

Refer Attached XML and import it in Process studio.

Run it against various test input scenarios and let me know what you feel about it.



Thanks VJR....
It's working Fine...
 

Patnaik14

Member
Usually, the start day of the week depends on the location. This gets tricky.

The below function accepts 3 arguments - Date, Start day of the week, number you want to add.
  • First argument is of type DateTime: Pass the date you want to consider for addition. In your case, its delivery date you want to change
  • Second Argument: I assumed Start Day of the week is Monday
  • Third Argument: Number of the date. 1- Monday, 2- Tuesday, 3- Wednesday, 4- Thursday...
Paste the below C# code in the Global code section of Initialize. (Change the language option to C#)

C#:
public static DateTime GetFirstDateOfWeek(DateTime dayInWeek, DayOfWeek firstDay, int i)
{
    DateTime firstDayInWeek = dayInWeek.Date;
    while (firstDayInWeek.DayOfWeek != firstDay)
        firstDayInWeek = firstDayInWeek.AddDays(-1);

    return firstDayInWeek.AddDays(7+ (i-1));
}

Now, add a Code Stage
  • Input:
    • DeliveryDate of Type DateTime
    • AddNumber of Type Number
  • Output:
    • NewDeliveryDate of Type DateTime
  • Code: As I said, I assumed the start of week as Monday and passed as an argument. If you wish to change this to Sunday, the code above needs little tweak to get the desired results.
    • NewDeliveryDate = GetFirstDateOfWeek(DeliveryDate , DayOfWeek.Monday, AddNumber)
Passing DeliveryDate as "19/06/2018" and AddNumber as 3 would result in NewDeliveryDate as "27/06/2018"

Post back how it goes!


Thanks SG...
 

Patnaik14

Member
Hi Patnaik14,

Basically the Pseudo logic for it would be as below.

Logic:
- Inputdate = 19/06/2018
- Day Count = 3

-Add 7 days to go to the next week (NextWeekDate = 26/06/2018)
-Find out the weekday number of this date (WeekdayNumber = 2 if Monday is 1)
I did not find a direct function to find the weekday number and hence used the Choice stages there. You can modify the Choice stages if you would like to have Sunday as the 1st day of week.

- Calculate the final result:
- If WeekdayNumber < Day Count then Add (Day Count - WeekdayNumber) to NextWeekDate and store it in Result
- If WeekdayNumber > Day Count then Sub (WeekdayNumber - Day Count) to NextWeekDate and store it in Result
- If WeekdayNumber = Day Count then No change needed as NextWeekDate is the final desired date and store it in Result

- Result data item will contain the final answer

Key pointers:
- It is not straight forward to explain the above logic via Blue Prism here and also the diagram does not fit clearly in a single screenshot, hence I have attached the xml of the diagram
- The two exception stages are just for your understanding and you need to handle those stages as per your requirements

Refer Attached XML and import it in Process studio.

Run it against various test input scenarios and let me know what you feel about it.
Hi Patnaik14,

Basically the Pseudo logic for it would be as below.

Logic:
- Inputdate = 19/06/2018
- Day Count = 3

-Add 7 days to go to the next week (NextWeekDate = 26/06/2018)
-Find out the weekday number of this date (WeekdayNumber = 2 if Monday is 1)
I did not find a direct function to find the weekday number and hence used the Choice stages there. You can modify the Choice stages if you would like to have Sunday as the 1st day of week.

- Calculate the final result:
- If WeekdayNumber < Day Count then Add (Day Count - WeekdayNumber) to NextWeekDate and store it in Result
- If WeekdayNumber > Day Count then Sub (WeekdayNumber - Day Count) to NextWeekDate and store it in Result
- If WeekdayNumber = Day Count then No change needed as NextWeekDate is the final desired date and store it in Result

- Result data item will contain the final answer

Key pointers:
- It is not straight forward to explain the above logic via Blue Prism here and also the diagram does not fit clearly in a single screenshot, hence I have attached the xml of the diagram
- The two exception stages are just for your understanding and you need to handle those stages as per your requirements

Refer Attached XML and import it in Process studio.

Run it against various test input scenarios and let me know what you feel about it.
=====================================================================================


The date is getting captured from SAP in TEXT format.
But before adding, I need this need to be convert to DATE format.
But it's not getting change.
Please assist
 
Top