0

I want to know how to find number of dates between two date by datetimepicker. for Example date range is from 2019.02.22 to 2019.06.19, and i have to find 23th date (2019.02.23/2019.03.23/2019.04.23/2019.05.23) i hope to get result to textbox as 4, however i tried by using below code and got result as 3, it is wrong answer. highly appreciate if anyone can help me on this.

        DateTime d5 = dateTimePicker1.Value;
        DateTime d6 = dateTimePicker3.Value;

        int MonthsElapsednew = ((d5.AddDays(1).Year - d6.AddDays(1).Year) 
        * 12) +(d5.AddDays(1).Month - d6.AddDays(1).Month) 
        (d5.AddDays(1).Day < d6.AddDays(1).Day ? 1 : 0);

        textBox16.Text = MonthsElapsednew.ToString();
Filburt
  • 17,626
  • 12
  • 64
  • 115
  • possible duplicate:https://stackoverflow.com/questions/36788857/how-to-get-dates-between-two-dates-in-c-sharp – Atul Mathew Jun 25 '19 at 08:01
  • Are you trying to find month count between two dates? – SᴇM Jun 25 '19 at 08:01
  • Possible duplicate: [difference-in-months-between-two-dates](https://stackoverflow.com/questions/4638993/difference-in-months-between-two-dates) – Hans Kapitein Jun 25 '19 at 08:06
  • 1
    I understand your question is that you want to count how many 23rd days of a month occur in the months between your start and end date. If your date was the 30th instead, what do we do with February? – ProgrammingLlama Jun 25 '19 at 08:08
  • @John, yes, You understood my question, how to solve this?, yes February is the questionable – Roshan Abrew Jun 25 '19 at 08:11
  • 2
    @RoshanAbrew, with all due respect, you are the one who needs to tell us the requirements. You need to tell us whether you want an `Exception` thrown, or else call the next day in the month (i.e. if 30th January is chosen for d5, you could output 31st January and 1st February). – Mark Cilia Vincenti Jun 25 '19 at 08:13
  • @MarkCiliaVincenti, if i select 2019.02.23 for d5 and 2019.06.19 for d6, there are 04 23rd days (2019.02.23/2019.03.23/2019.04.23/2019.05.23), however i can not get correct result above code. – Roshan Abrew Jun 25 '19 at 08:24
  • @RoshanAbrew, you didn't answer John's question really, which was what I was referring to. Also, there is a mismatch between what you wrote in the above comment and what you wrote in the question. One picks 23rd when the 22nd is selected, and in the comment you said you pick 23rd when the 23rd is selected. – Mark Cilia Vincenti Jun 25 '19 at 08:26
  • @MarkCiliaVincenti, ok, assume that i select start date 2019.02.23 – Roshan Abrew Jun 25 '19 at 08:31
  • @RoshanAbrew, what do you want to happen if the start date selected is 2019-01-31 and the end date is 2019-12-31? I see three options: either throw an `Exception`, skip months that do not contain a 31st, or choose the following day (1st) for months like February, April, etc. – Mark Cilia Vincenti Jun 25 '19 at 08:33
  • @MarkCiliaVincenti, Thanks, at that time we consider last day of the month, we are manually calculating, however at programming, we face difficulties due to 30th and 31st or February, i understood now, can you send me your options by code.. ? – Roshan Abrew Jun 25 '19 at 08:45
  • @RoshanAbrew, no, choose one and I'll help, but I'm not going to do triple the work until you decide which option to go for. – Mark Cilia Vincenti Jun 25 '19 at 08:55

1 Answers1

2

Building on How to get dates between two dates in C# it's as simple as this:

DateTime startdate = new DateTime(2019, 2, 22);
DateTime enddate = new DateTime(2019, 6, 19);

int monthselapsed = Enumerable
    .Range(0, int.MaxValue)
    .Select(index => new DateTime?(startdate.AddDays(index)))
    .TakeWhile(date => date <= enddate)
    .Where(day => day.Value.Day == 23)
    .Count();
Filburt
  • 17,626
  • 12
  • 64
  • 115
  • @Filburt, Thanks, i am using datetimepicker and how to apply for your code. please consider start date 2019.02.23 and find total days upto 2019.06.19, it must be 04, 2019.02.23/2019.03.23/2019.04.23/2019.05./23 – Roshan Abrew Jun 25 '19 at 08:29
  • @RoshanAbrew **how to apply for your code** I think, the answer is clear. – Avinash Reddy Jun 25 '19 at 08:53
  • @MarkCiliaVincenti I don't see any assumptions here - admittedly the question and title have not been formulated to the best but the desired result is absolutely clear. – Filburt Jun 25 '19 at 08:55
  • @Filburt, refer to the comments on the question. The requirements are still not clear, hence an answer will always be premature, notwithstanding the fact that the answer itself is hardcoded. – Mark Cilia Vincenti Jun 25 '19 at 08:56
  • @RoshanAbrew I just changed your `d5` and `d6` variables to have more descriptive names - you should be able to apply this without problems. – Filburt Jun 25 '19 at 08:57
  • The `new DateTime?()` is not needed – Hans Kapitein Jun 25 '19 at 09:02
  • 1
    @MarkCiliaVincenti It is important that the OP learns to understand the code and apply handling for all the edge cases. It's not helpful to try and deliver a ready made solution basically doing all the OP's work. – Filburt Jun 25 '19 at 09:31