How to dynamically pick the date from the below mentioned format


How to pick the date (last month end of the month)in this and if it is saturday or sunday then it should take previous date

Hey @anjani_priya
Step 1: Get the last day of the previous month
Assign Activity:**

  • To: lastMonthEndDate (Variable of type DateTime)
  • Value:
New DateTime(Now.Year, Now.Month, 1).AddDays(-1)

Step 2: Adjust if it’s a Saturday or Sunday

Assign Activity:

  • To: adjustedDate (Variable of type DateTime)
  • Value:
If lastMonthEndDate.DayOfWeek = DayOfWeek.Saturday Then
    lastMonthEndDate.AddDays(-1)
ElseIf lastMonthEndDate.DayOfWeek = DayOfWeek.Sunday Then
    lastMonthEndDate.AddDays(-2)
Else
    lastMonthEndDate
End If

cheers

Hi @anjani_priya

You could use the following Assign Activity code to obtain the day:

lastDay = If(New DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).AddDays(-1).DayOfWeek = System.DayOfWeek.Saturday OrElse New DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).AddDays(-1).DayOfWeek = System.DayOfWeek.Sunday, New DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).AddDays(-2).ToString("dd-MM-yyyy"), New DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).AddDays(-1).ToString("dd-MM-yyyy"))

The Output

If this solves your issue, Do mark it as a solution
Happy Automation :star_struck:

How to indicate it dynamically

How to indicate it dynamically.

Step 1: Calculate the Target Date in Assign Activity**

Use this logic in an Assign activity to get the last business day of the previous month:

lastDate = New DateTime(Now.Year, Now.Month, 1).AddDays(-1)

targetDate = If(lastDate.DayOfWeek = DayOfWeek.Saturday, lastDate.AddDays(-1),
           If(lastDate.DayOfWeek = DayOfWeek.Sunday, lastDate.AddDays(-2), lastDate))
  • lastDate: Last day of previous month
  • targetDate: Adjusted date (if Sat/Sun → go back to Friday)

Both variables should be of type System.DateTime

Step 2: Format the Date (Optional)

If your calendar needs a specific format (like just the day number or a full date), use another Assign activity:

dayToClick = targetDate.Day.ToString()  ' For clicking just the day (like "31")

Step 3: Interact with the Calendar UI

This part depends on how your calendar is built. Here are 2 ways to handle it:

Option A: Use Dynamic Selector with Click Activity

If each day in the calendar is a clickable element with identifiable attributes (e.g., aaname="31"), use a Click activity with a dynamic selector.

Example:

<wnd app='yourApp.exe' ... />
<ctrl name='Calendar' ... />
<ctrl name='{{dayToClick}}' role='push button' />

In the Click activity:

  • Enable Selector
  • Use dayToClick variable in the selector like:

vb

"<ctrl name='" + dayToClick + "' role='push button' />"

cheers

1 Like

If you are talking about UI automation, then you could use Inject JS script activity

Hi @anjani_priya

First get the date

inputDateString = New DateTime(Now.Year, Now.Month, 1).AddDays(-1).AddDays(
If(New DateTime(Now.Year, Now.Month, 1).AddDays(-1).DayOfWeek = DayOfWeek.Saturday, -1, 
If(New DateTime(Now.Year, Now.Month, 1).AddDays(-1).DayOfWeek = DayOfWeek.Sunday, -2, 0))).ToString("dd MMMM yyyy")

Use select item acticity indicate it on the year dropdown for the year and pass the expression
inputDateString.Split(" "c)(2)

For month
use while loop

  • Use get text and indicate it on the month
  • If it is equal to inputDateString.Split(" "c)(1) use break activity to break from the loop
  • Else click on the next arrow mark

For date
date = inputDateString.Split(" "c)(0)
Use click activity
In the dynamic selector add the variable date

Hope this helps!

1 Like

Hey @anjani_priya

Use the following code in assign activity

If( New DateTime(Now.Year, Now.Month, 1).AddDays(-1).DayOfWeek = DayOfWeek.Sunday OrElse New DateTime(Now.Year, Now.Month, 1).AddDays(-1).DayOfWeek = DayOfWeek.Saturday,
New DateTime(Now.Year, Now.Month, 1).AddDays(-2).ToString(“yyyy-MM-dd”),
New DateTime(Now.Year, Now.Month, 1).AddDays(-1).ToString(“yyyy-MM-dd”)
)

For choosing the date dynamically use can tweak the selector using attributes like aaname or innertext

Hope this helps!

Hi @anjani_priya

If you are asking dynamically based on Input Date. Here is one approach:

day = "13-12-2024"
dayDateTime = DateTime.ParseExact(day, "dd-MM-yyyy", System.Globalization.CultureInfo.InvariantCulture)
lastDay = New DateTime(dayDateTime.Year, dayDateTime.Month, 1).AddDays(-1)
lastDayOfMonth = If( lastDay.DayOfWeek = System.DayOfWeek.Saturday OrElse lastDay.DayOfWeek = System.DayOfWeek.Sunday, lastDay.AddDays(-1).ToString("dd-MM-yyyy"), lastDay.ToString("dd-MM-yyyy"))

There is no need of 4 different variables. You can manage with just 2. But i have made 4 for readability:

THE OUTPUT:

If this solves your issue, Do mark it as a solution
Happy Automation :star_struck:

1 Like

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