How to Extract Date for last year Week

Hello Team,

Based on Current Date it should extract last year’s date(Monday/Sunday)

Example
If today is 16th 2024

Then it should Extract Last week date for Last years 2023(Monday and Sunday)

It should check In 2023
For Example 16th March the last week date should be selected

Example below value should be fetched (Monday and Sunday)

6th March (Monday)
12th March (Sunday)

Thanks in advance

Hi @NISHITHA ,

Try below flow, hope it works for you

BlankProcess3.zip (4.1 KB)

Regards,
Vinit Mhatre

Hello @Vinit_Mhatre

Is there some other approach am not able to run the xaml

Thanks for reference

Hi @NISHITHA ,

Can you tell me what error is facing you?

Regards,
Vinit Mhatre

Hi @Vinit_Mhatre

Getting this exception in xaml file

image (9)

Hi @NISHITHA ,

If you are not able to open my workflow then try to repair the dependencies or create a workflow as below

A) Take assign activity for and add as below: 
1. Previousyeardate as DateTime = DateTime.Now.AddYears(-1)
2. dayOfWeek as DayOfWeek = previousYearDate.DayOfWeek

B) Take Else-if activity

Condition : dayOfWeek = DayOfWeek.Monday
Then : assign lastSunday as DateTime = previousYearDate.AddDays(-1)
Else-if condition: dayOfWeek = DayOfWeek.Tuesday
Then : assign lastSunday as DateTime = previousYearDate.AddDays(-2)
Else-if condition: dayOfWeek = DayOfWeek.Wednesday
Then : assign lastSunday as DateTime = previousYearDate.AddDays(-3)
Else-if condition: dayOfWeek = DayOfWeek.Thursday
Then : assign lastSunday as DateTime = previousYearDate.AddDays(-4)
Else-if condition: dayOfWeek = DayOfWeek.Friday
Then : assign lastSunday as DateTime = previousYearDate.AddDays(-5)
Else-if condition: dayOfWeek = DayOfWeek.Saturday
Then : assign lastSunday as DateTime = previousYearDate.AddDays(-6)
Else-if condition: dayOfWeek = DayOfWeek.Sunday
Then : assign lastSunday as DateTime = previousYearDate.AddDays(-7)

C) Take assign and create variable Lastmonday as DateTime
Assign: lastMonday as Datetime = lastSunday.AddDays(-6)

You will get output for Monday and sunday in below variable 
"Monday: "+lastMonday.ToString("dd/MM/yyyy")+vbCrLf+"Sunday: "+lastSunday.ToString("dd/MM/yyyy")






Regards,
Vinit Mhatre

Hello @NISHITHA , You can do this in 3 ways using UiPath.

  • UiPath Studio
  • Custom Activity or C# or Vb Code
  • Push Data in data base via UiPath using SQL Query you can achieve this task. [My Recommendation but you try first two]

This workflow utilizes built-in UiPath activities for a visually clear and intuitive solution:

Professional Solutions for Extracting Last Week’s Dates (Monday & Sunday) from Last Year

This document presents three professional approaches to extract the previous week’s Monday and Sunday dates from last year, considering the current date:

1. UiPath Workflow:

Prerequisites:

  • UiPath Studio (licensed version recommended)
  • Familiarity with UiPath activities

Solution:

This workflow utilizes built-in UiPath activities for a visually clear and intuitive solution:

  1. Get Today’s Date:

    • Employ the Get System Date activity to retrieve the current system date and store it in a variable named currentDate.
  2. Calculate Last Year’s Date:

    • Utilize the Add Years activity to subtract one year from currentDate. Assign the result to a variable named lastYearDate.
  3. Get Week Start and End Dates:

    • Apply the Get Week Start and End Dates activity:
      • Input lastYearDate as the base date.
      • Set OutputFormat to yyyy-MM-dd (or a preferred format) for consistent date representation.
      • Store the output in variables: lastWeekStartDate for Monday and lastWeekEndDate for Sunday.

Explanation:

  • The Get System Date activity ensures the workflow reflects the current date.
  • The Add Years activity accurately calculates the corresponding date from last year.
  • The Get Week Start and End Dates activity efficiently extracts the previous week’s start (Monday) and end (Sunday) dates with user-defined formatting.

Output:

The workflow outputs the lastWeekStartDate and lastWeekEndDate, representing the previous Monday and Sunday of last year.

Benefits:

  • Visually clear and easy to maintain in UiPath Studio.
  • User-friendly date format customization.
  • Built-in activities for robust date manipulation.

2. Custom Activity Code(C# or VB):

Prerequisites:

  • A C# development environment (e.g., Visual Studio)
  • Solid understanding of C# data/time functionalities
  • know about convert DLL to UiPath Packages

Solution:

This code snippet provides a well-structured approach:

using System;

public class LastWeekDates
{
    public static void Main(string[] args)
    {
        DateTime currentDate = DateTime.Today;
        DateTime lastYearDate = currentDate.AddYears(-1);

        DateTime lastWeekStartDate = lastYearDate.AddDays(-(int)currentDate.DayOfWeek);
        DateTime lastWeekEndDate = lastWeekStartDate.AddDays(6);

        Console.WriteLine("Last Week Start Date (Monday): {0:yyyy-MM-dd}", lastWeekStartDate);
        Console.WriteLine("Last Week End Date (Sunday): {0:yyyy-MM-dd}", lastWeekEndDate);
    }
}

Explanation:

  1. The code leverages DateTime.Today to retrieve the current date.
  2. AddYears(-1) calculates the last year’s date accurately.
  3. AddDays(-(int)currentDate.DayOfWeek) adjusts lastYearDate to the previous Monday.
  4. AddDays(6) adds six days to get the previous Sunday.
  5. Formatted dates are displayed using Console.WriteLine with clear labels.

Benefits:

  • Well-structured and readable code.
  • Efficient date manipulation using C# functionalities.
  • Flexibility for further processing or integration.

3. Database (SQL Server Example):

Prerequisites:

  • A SQL Server database management system
  • Proficiency in writing SQL queries

Solution:

This SQL query implements efficient date calculations:

DECLARE @currentDate DATE = GETDATE();
DECLARE @lastYearDate DATE = DATEADD(YEAR, -1, @currentDate);

SELECT
    DATEADD(DAY, -DATEPART(dw, @lastYearDate), @lastYearDate) AS LastWeekStartDate,
    DATEADD(DAY, 6 - DATEPART(dw, @lastYearDate), @lastYearDate) AS LastWeekEndDate

Explanation:

  1. The query declares variables for the current and last year’s dates.
  2. DATEADD(YEAR, -1, @currentDate) calculates the date from last year.
  3. DATEPART(dw, @lastYearDate) extracts the day of the week from lastYearDate.
  4. DATEADD(DAY, -DATEPART(dw, @lastYearDate), @lastYearDate) adjusts lastYearDate to the previous Monday.
  5. DATEADD(DAY, 6 - DATEPART(dw, @lastYearDate), @lastYearDate) adds six days to get the previous Sunday.
  6. The query

@NISHITHA I was just given a example of test cases but you can try this your own and know you should got the idea how to solve this.

Thanks @Vinit_Mhatre

For the Detailed Steps to be performed.

Now its Working.

Could you just summarize what is being done in Step B)

Thanks once again for the solution

Thanks @mukesh.singh for explaining different approaches in a detailed way

Hi @NISHITHA ,

Step B:

  1. Else-if Activity:
  • This activity is used to handle different cases based on the day of the week obtained in Step A.
  • It starts with a condition for Monday and then checks each day of the week.
  • For each day of the week:
  • If the condition matches (dayOfWeek = DayOfWeek.Monday, dayOfWeek = DayOfWeek.Tuesday, etc.), it executes the associated action.
  • The action involves assigning a value to the LastSunday variable. This value is calculated based on the PreviousYearDate.
  • For example, if it’s Monday, it sets LastSunday to the date one day before the PreviousYearDate.
  • It continues this pattern for each day of the week.

Regards,
Vinit Mhatre

Thanks @Vinit_Mhatre

1 Like

Hi @NISHITHA ,

Welcome !!!
Mark as solution. If you get your response, it will also benefit others.

Happy Automation !!! :slight_smile:

Regards,
Vinit Mhatre

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