An example of how IOS gets the number of days of the month of a year

Source: Internet
Author: User

In a one-month interval for the first and last day, you need to know how many days of the month you have to know what the last day is, and whether it is a leap year, maybe 28 days in February.
Attached code:


-(void) Viewdidload {
[Super Viewdidload];
Do no additional setup after loading the view, typically from a nib.
NSLog (@ "%ld", (long) [self howmanydaysinthisyear:2016 withmonth:1]);
NSLog (@ "%ld", (long) [self howmanydaysinthisyear:2016 withmonth:2]);
NSLog (@ "%ld", (long) [self howmanydaysinthisyear:2016 withmonth:3]);
NSLog (@ "%ld", (long) [self howmanydaysinthisyear:2016 withmonth:4]);
NSLog (@ "%ld", (long) [self howmanydaysinthisyear:2016 withmonth:5]);
NSLog (@ "%ld", (long) [self howmanydaysinthisyear:2016 withmonth:6]);
NSLog (@ "%ld", (long) [self howmanydaysinthisyear:2016 withmonth:7]);
NSLog (@ "%ld", (long) [self howmanydaysinthisyear:2016 withmonth:8]);
}
#pragma mark-Get the number of days of the month of a year
-(Nsinteger) Howmanydaysinthisyear: (Nsinteger) Year Withmonth: (Nsinteger) month{
if ((month = 1) | | (month = 3) | | (month = 5) | | (month = 7) | | (month = 8) | | (month = 10) | | (month = 12))
return 31;

if ((month = 4) | | (month = 6) | | (month = 9) | | (month = 11))
return 30;

if (year% 4 = 1) | | (Year% 4 = 2) | | (Year% 4 = 3))
{
return 28;
}

If (year% 400 = 0)
return 29;

if (year% 100 = 0)
return 28;

return 29;
}

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.