When I was viewing RSS, I found such an article in C-sharpcorner,
Find the first and last days of the month with C #
I used to study a kind of mathematics.AlgorithmI was prepared to read this document, but found that it was not the result I wanted.
In fact, this foreigner is a bit outdated. If you want to learn an implementation method, you can see how to use subtraction to find the earliest and last day of a month! However, if you use a. net function library, the following method may be simpler.
Obtain the earliest and last days of a month. In the datetime class of C #, ready-made functions are provided. The following two simple functions are used:
Private Datetime getfirstdayofmonth ( Int Year, Int Month)
{
// Have you ever seen a month starting from day 1? No
// Then, directly return it to the caller!
// A good programming habit is yours.CodeEasy to understand
Return Convert. todatetime (Year. tostring () + " - " + Month. tostring () + " -1 " );
}
Private Datetime getlastdayofmonth ( Int Year, Int Month)
{
// The key here is datetime. daysinmonth to get the number of days in a month.
Int Days = Datetime. daysinmonth (year, month );
Return Convert. todatetime (Year. tostring () + " - " + Month. tostring () + " - " + Days. tostring ());
}
In the above two functions, I did not write as many reloads as the previous author. I believe that you will implement them on your own as needed. In addition, I have not verified the input parameters, but I suggest you verify the validity of the parameters!
The datetime class also has a datetime. the isleapyear (INT year) function is used to determine whether a year is a leap year. In the future, you do not need to write a multiples of 4, and the method for determining the leap year by dividing 100.
Enjoy. net!