How to split the given date range month wise

Hi,

Dates are in MM/dd/yyyy format
Start Date: 05/25/2025
End Date : 08/31/2025.
Split the date based on month wise which include start and end date of each month like below.

Output:(split based on month)
05/25/2024 00:00:00, 05/31/2024 00:00:00),
(06/01/2024 00:00:00, 06/30/2024 00:00:00),
(07/01/2024 00:00:00, 07/31/2024 00:00:00),
(08/01/2024 00:00:00, 08/31/2024 00:00:00)

TIA

Hi @Manaswini_UI ,

You can achieve this using a combination of Assign activities and a While loop to split the date range month-wise. Here’s a quick solution:

  1. First, create variables:

    • startDate (DateTime) = DateTime.ParseExact("05/25/2025", "MM/dd/yyyy", System.Globalization.CultureInfo.InvariantCulture)
    • endDate (DateTime) = DateTime.ParseExact("08/31/2025", "MM/dd/yyyy", System.Globalization.CultureInfo.InvariantCulture)
    • currentStart (DateTime) = startDate
    • currentEnd (DateTime)
    • resultList (List of String) = New List(Of String)
  2. Use a While loop with the condition currentStart <= endDate.

  3. Inside the loop:

    • Set currentEnd to the last day of the current month using:
      currentEnd = New DateTime(currentStart.Year, currentStart.Month, DateTime.DaysInMonth(currentStart.Year, currentStart.Month))
      
    • If currentEnd > endDate, adjust it:
      If currentEnd > endDate Then
          currentEnd = endDate
      End If
      
    • Add the date range to resultList:
      resultList.Add($"({currentStart.ToString("MM/dd/yyyy")} 00:00:00, {currentEnd.ToString("MM/dd/yyyy")} 00:00:00)")
      
    • Move to the next month:
      currentStart = currentEnd.AddDays(1)
      
  4. Finally, log or display the result:

    String.Join(Environment.NewLine, resultList)
    

Sample Output:

(05/25/2025 00:00:00, 05/31/2025 00:00:00)
(06/01/2025 00:00:00, 06/30/2025 00:00:00)
(07/01/2025 00:00:00, 07/31/2025 00:00:00)
(08/01/2025 00:00:00, 08/31/2025 00:00:00)

This should split your date range based on each month while including the start and end dates as requested. Let me know if this helps! :blush:

Cheers!

@Manaswini_UI

please try this

Enumerable.Range(0, Integer.MaxValue).
    Select(Function(i) New DateTime(startDate.Year, startDate.Month, 1).AddMonths(i)).
    TakeWhile(Function(d) d <= endDate).
    Select(Function(d) New Tuple(Of DateTime, DateTime)(
        If(d < startDate, startDate, d), 
        If(d.AddMonths(1).AddDays(-1) > endDate, endDate, d.AddMonths(1).AddDays(-1))
    )).ToList()

image

cheers

@Manaswini_UI

loop them using for loop and can use add data row inside to add the data as rows in datatable

cheers

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