How to split the dates with given Range

Hi,

Dates are in MM/dd/yyyy format
Start Date: 05/25/2025
End Date : 08/31/2025.
Need to split the date based on given input like 15 days/1 month and generate start and end for each interval like below.

Output:(split based on month)
From Date:05/25/2025 To date:05/31/2025
From Date:06/01/2025 To date:06/30/2025
From Date:07/01/2025 To date:07/31/2025
From Date:08/01/2025 To date:08/31/2025

Output:(split based on 15days gap)
From Date:05/25/2025 To date:06/09/2025
From Date:06/10/2025 To date:06/25/2025
From Date:06/26/2025 To date:07/11/2025
From Date:07/12/2025 To date:07/27/2025
From Date:07/28/2025 To date:08/12/2025
From Date:08/13/2025 To date:08/28/2025
From Date:08/29/2025 To 08/31/2025

TIA

You can try using this LINQ filter to get the necessary data between specified dates.

dtReport.AsEnumerable.Where(Function(drRow) drRow.Field(Of DateTime)("Date") >= dtmStartDate AndAlso drRow.Field(Of DateTime)("Date") <= dtmEndDate).CopyToDataTable

Let me know if you have further questions.
I hope this helps.

Cheers!

@Manaswini_UI

this generate a list of tuple…that is each list item containign two dates one start and one end

Enumerable.Range(0, Integer.MaxValue).
                                   Select(Function(i) startDate.AddDays(i*15)).
                                   TakeWhile(Function(d) d <= endDate).
                                   Select(Function(d) New Tuple(Of DateTime, DateTime)(d, If(d.AddDays(15) > endDate, endDate, d.AddDays(15)))).
                                   ToList()

this is for 15 days if you need month use 30 or addmonths inplace of adddays

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

usage

cheers

List<Tuple<DateTime, DateTime>>(7) { (05/25/2024 00:00:00, 06/24/2024 00:00:00), (06/09/2024 00:00:00, 07/09/2024 00:00:00), (06/24/2024 00:00:00, 07/24/2024 00:00:00), (07/09/2024 00:00:00, 08/08/2024 00:00:00), (07/24/2024 00:00:00, 08/23/2024 00:00:00), (08/08/2024 00:00:00, 09/07/2024 00:00:00), (08/23/2024 00:00:00, 08/31/2024 00:00:00) }
To date column showing incorrect value
05/25/2024+15 days=06/09/2024

@Manaswini_UI

Edited the formula above instead of d.Adddays(30) use d.adddays(15)

Use thw edited formula from above comment

Cheers

Thankyou its working but can we do small change in formula to get the the values like below
From date in 2nd row starts from 06/10/2025 (06/09/2024 covered in first row Todate) like wise other dates also should change like below
Output:
From Date:05/25/2025 To date:06/09/2025
From Date:06/10/2025 To date:06/25/2025
From Date:06/26/2025 To date:07/11/2025
From Date:07/12/2025 To date:07/27/2025
From Date:07/28/2025 To date:08/12/2025
From Date:08/13/2025 To date:08/28/2025
From Date:08/29/2025 To 08/31/2025

if possible, Can you please check and update the query

@Manaswini_UI

here is a small modified code

Enumerable.Range(0, Integer.MaxValue).
                                   Select(Function(i) startDate.AddDays(i*16)).
                                   TakeWhile(Function(d) d <= endDate).
                                   Select(Function(d) New Tuple(Of DateTime, DateTime)(d, If(d.AddDays(15) > endDate, endDate, d.AddDays(15)))).
                                   ToList()

cheers

Thankyou, its working

1 Like

can we get the dates like below start date and end date of each month from given date range

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)

can we split the dates month wise from given start and end date like below

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:0

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