find days in current month in csharp

Here's the code to find the number of days in the current month using C#:

main.cs
int daysInMonth = DateTime.DaysInMonth(DateTime.Now.Year, DateTime.Now.Month);
79 chars
2 lines

This code will return the number of days in the current month based on the current date and time. The DateTime.Now.Year and DateTime.Now.Month properties are used to get the current year and month, respectively. The DaysInMonth method then takes these parameters to determine the number of days in the specified month.

You can also specify a specific date by passing a DateTime object that represents that date to the DaysInMonth method, like this:

main.cs
DateTime date = new DateTime(2022, 12, 1);
int daysInMonth = DateTime.DaysInMonth(date.Year, date.Month);
106 chars
3 lines

This code will return the number of days in December 2022.

gistlibby LogSnag