Get all the month names along with number of days in them between a date range

Hello,

I will be having a dynamic date range(from & To date), I need to find all the month names & number of days in between that date range, the from & To date may or may not be a full month, so the no. of days should be calculated accordingly.

Sample, Input & Output below

image

image
I tired looking through all similar topics but couldn’t find an exact one, apologies if it is a duplicate one.

I am trying to build a solution using while loop unsuccessfully, wondering if there is any LINQ for such scenarios.

Any suggestion will be very helpful.

Thanks

1 Like

Hi,

I just wrote simple sample code using while loop and dictionary. Hope the following helps you.

Main.xaml (9.7 KB)

Regards,

1 Like

@Yoichi Thanks a lot, this works, though I had to spend some time to understand the steps especially the operations being performed in Invoke method, if you could please add one line descriptions as well, for instance, how is the method calculating the number of days of the partial month?, it might be helpful for others also:-).
Secondly, my input dates will be in format MM/dd/YYYY, if I tried this format for partial end date, the output was wrong, instead of giving partial number of days it was giving total number of days for the month.

image

Thanks again for the help.

Hi,

if you could please add one line descriptions as well, for instance, how is the method calculating the number of days of the partial month?, it might be helpful for others also:-).

Thank you for your advice. The above logic is as follows:

From the start date to the end date, adding day one by one. Then value of dictionary the month of the day as key is added 1 to dictionary.

We can convert the string to DateTime type using DateTime.ParseExact method as the following.

endDate = DateTime.ParseExact("07/03/2020","MM/dd/yyyy",nothing)

Regards,

2 Likes