IOS gets the date n months after a date, and ios gets the date

Source: Internet
Author: User

IOS gets the date n months after a date, and ios gets the date
1. Give a time and a number. The positive number is the next n months, and the negative number is the previous n months;

 1 -(NSDate *)getPriousorLaterDateFromDate:(NSDate *)date withMonth:(NSInteger)month 2   3 { 4   5     NSDateComponents *comps = [[NSDateComponents alloc] init]; 6   7     [comps setMonth:month]; 8   9     NSCalendar *calender = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];10  11     NSDate *mDate = [calender dateByAddingComponents:comps toDate:date options:0];12  13  14     return mDate;15  16 }

 

2. Set the year, month, and day you want to increase or decrease to get a new date. The above indicates to get the date of the month before mydate. If it is set to + 1, it is the date after one month, and so on.
 1 - (NSDate *)getLaterDateFromDate:(NSDate *)date withYear:(NSInteger)year month:(NSInteger)month day:(NSInteger)day { 2     NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian]; 3   4     NSDateComponents *comps = nil; 5   6     comps = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:date]; 7   8     NSDateComponents *adcomps = [[NSDateComponents alloc] init]; 9  10     [adcomps setYear:year];11  12     [adcomps setMonth:month];13  14     [adcomps setDay:day];15  16     NSDate *newdate = [calendar dateByAddingComponents:adcomps toDate:date options:0];17  18     return newdate;19 }

 

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.