Extract Last Week and Second Last Week

Hello Team,

We are Extracting For Last Week and Last to Last week dates
From Monday to Sunday

Example 1

If Today’s Date was 12-02-2024

  1. Last week date that we need to extract

DayMonday : 05-02-2024
DaySunday : 11-02-2024

Second Last week we need to extract in below format (Contains both Last Month and This Month)

So we perform it Twice for Monday to Sunday because it contains Last Month and this Month

To Split Date

Second Last week date to extract

Last Month
DayMondaylastmonth : 29-01-2024 (Start from Monday)
DaySundaylastmonth : 31-01-2024 (Last day Of Month)

This Month

DayMondaySecond : 01-02-2024 (Start Of Month)
DaySundaySecond : 04-02-2024 (Till Sunday)

The Split Condition We need to add if we have two different Month (Monday and Sunday)

Example 2
If today was 19th Feb then split need not be applied

  1. Date Extraction Last Week
    DateOfLastWeekFirst : 12-02-2024 (Monday)
    DateOfLastWeekSecond : 18-02-2024 (Sunday)

2.Date Extraction Second Last Week
DateOfLastWeekSecondLastWeekFirst :05-02-2024 (Monday)
DateOfLastWeekSecondLastWeekEnd : 11-02-2024 (Sunday)

Thanks team in advance

Hey @NISHITHA

  1. Last Week’s Monday and Sunday
lastWeekMonday = DateTime.Now.AddDays(-(DateTime.Now.DayOfWeek - DayOfWeek.Monday) - 7)
lastWeekSunday = lastWeekMonday.AddDays(6)
  1. Second Last Week’s Monday and Sunday
secondLastWeekMonday = DateTime.Now.AddDays(-(DateTime.Now.DayOfWeek - DayOfWeek.Monday) - 14)
secondLastWeekSunday = secondLastWeekMonday.AddDays(6)

@pikorpa thanks for Reference

Is it possible to determine if the start and end month are different

And then perform the operation accordingly

Example If today was 12-02-2024

Then Date Extracted would be

  1. To Split Date if (Monday and Sunday contain different Month)

Second Last week date to extract

Last Month
DayMondaylastmonth : 29-01-2024 (Start from Monday)
DaySundaylastmonth : 31-01-2024 ( Till Last day Of Month)

This Month

DayMondaySecond : 01-02-2024 (Start Of Month)
DaySundaySecond : 04-02-2024 (Till Sunday)

Otherwise to perform Normally

The Date Extraction of Monday to Sunday
(For Last / Second Last Week)

@NISHITHA

Sure, try this:

  • if statement
    secondLastWeekMonday.Month <> secondLastWeekSunday.Month

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