how could i programmatically detect, how many days are there for a particular month & year.
How to Convert Months to Days. To convert a month measurement to a day measurement, multiply the time by the conversion ratio. The time in days is equal to the months multiplied by 30.436875.
1. Select a blank cell you will place the counting result into, and type the formula =DAY(DATE(YEAR(B1),MONTH(B1)+1,)) (B1 is the cell with the given date) into it, and press the Enter key. And then you will get the total number of days in the specified month based on a given date.
Now you need to change the result cell to date format with clicking Home > Number Format box > Short Date. See screenshot. 4. Keep selecting the result cell, then drag the Fill Handle down to list all days as date of this specified month.
It's already there:
DateTime.DaysInMonth(int year, int month);
should do it.
Something like this should do what you want:
static int GetDaysInMonth(int year, int month) { DateTime dt1 = new DateTime(year, month, 1); DateTime dt2 = dt1.AddMonths(1); TimeSpan ts = dt2 - dt1; return (int)ts.TotalDays; }
You get the first day of the month, add one month and count the days in between.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With