2

What I'm trying to achieve

I'm trying to get the number of days of two months (the current) and the next month. Actually I succesfully achieve this, using that code:

int monthDays = DateTime.DaysInMonth(DateTime.Now.Year, DateTime.Now.Month);
string[] days = Enumerable.Range(1, monthDays).Select(x => x.ToString("D2")).ToArray();

essentially I used the function DaysInMonth and then I generated a List<int> that represents the days of that month.

Problem

Now, I want get also the days of the next month, but I've some problem to handle the following situation:

December 2018 (current)
January 2019 (next)

What I tried

as you can see the year has changed, so the code that I wrote for get the days of next months will fail:

var nextMonth = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).AddMonths(1);
monthDays = DateTime.DaysInMonth(DateTime.Now.Year, nextMonth.Month);
days = Enumerable.Range(1, monthDays).Select(x => x.ToString("D2")).ToArray();

how can I manage the new year in the next month?

Fuzzybear
  • 1,388
  • 2
  • 25
  • 42
Charanoglu
  • 1,229
  • 2
  • 11
  • 30
  • 5
    You know how you're accessing `nextMonth.Month` to obtain *its* month rather than using `DateTime.Now`? Why aren't you doing the same for `Year`? – Damien_The_Unbeliever Jul 27 '18 at 11:56
  • 1
    As an aside, it's **january** :) – Flater Jul 27 '18 at 11:57
  • as @Damien_The_Unbeliever suggested, use `monthDays = DateTime.DaysInMonth(nextMonth.Year, nextMonth.Month);` – Liu Jul 27 '18 at 11:58
  • what is your definition of "the days in the month"? Like Mon, Tues... or 1,2,3? – Liam Jul 27 '18 at 11:58
  • 1
    @Liam 31 days in January, 28/29 days in Feb.... – Liu Jul 27 '18 at 11:59
  • @Damien_The_Unbeliever I was looking for another way to achieve that (my solution is heavy enough for a simple thing) – Charanoglu Jul 27 '18 at 11:59
  • Possible duplicate of [Getting number of days in a month](https://stackoverflow.com/questions/4832468/getting-number-of-days-in-a-month) – Liam Jul 27 '18 at 11:59
  • Cos you know that this is just in the framework [`DateTime.DaysInMonth`](https://stackoverflow.com/questions/4832468/getting-number-of-days-in-a-month) – Liam Jul 27 '18 at 12:00
  • @Charanoglu - you literally *type less* to use `nextMonth.Year` in that final call than `DateTime.Now.Year`. – Damien_The_Unbeliever Jul 27 '18 at 12:00
  • 1
    @Liam yep.. I overlooked it.. – Charanoglu Jul 27 '18 at 12:00
  • 1
    Also, I'm guessing you avoided doing `DateTime.Now.AddMonths(1)` to avoid overflows? Be aware that [AddMonths](https://msdn.microsoft.com/en-us/library/system.datetime.addmonths(v=vs.110).aspx#Remarks) already takes care of this: "If the resulting day is not a valid day in the resulting month, the last valid day of the resulting month is used. " – Damien_The_Unbeliever Jul 27 '18 at 12:04
  • @Damien_The_Unbeliever yep, I had a bit of trouble in the past using it – Charanoglu Jul 27 '18 at 12:06

2 Answers2

6

Use AddMonth() to ... add a month. Finally, use DaysInMonth to get the number of days in the specified month and year.

public static void Main()
{
    // 12 for december as example
    var current = new DateTime(DateTime.Now.Year, 12, DateTime.Now.Day);
    var next  = current.AddMonths(1);
    Console.WriteLine(DateTime.DaysInMonth(next.Year, next.Month));
}

Output

31

Try it Online!

Bonus: you can read the AddMonth() source.

aloisdg
  • 22,270
  • 6
  • 85
  • 105
-2
    public int GetDaysInMonth(DateTime date)
    {
        return DateTime.DaysInMonth(date.Year, date.Month);
    }

    public int GetDaysInNextMonth(DateTime date)
    {
                    //Adding months will automatically sort out the year if need be
        var nextMonth = date.AddMonths(1);
        return DateTime.DaysInMonth(nextMonthDate.Year, nextMonthDate.Month);
    }
Gerald Chifanzwa
  • 1,277
  • 7
  • 14
  • What does the ensuring do here? You never use those vars? Why do you want to *Ensure we are dealing with day 1 of month to take care of those 30, 31 days*? The baked in method does all this for you if your trying to throw exceptions – Liam Jul 27 '18 at 12:11
  • @Liam good point. AddMonths() doesnt overflow to the next month if next month has less days...had missed that one – Gerald Chifanzwa Jul 27 '18 at 12:21