C # obtain the first and last days of the month and determine the leap year.

Source: Internet
Author: User

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!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.