Calculates the number of days between a date and the current date.

Source: Internet
Author: User

Calculates the number of days between a date and the current date.

// Returns the day 0, Day 1, Day 2, and so on.

// The time difference is used to divide the number of seconds of a day, and the calculated number of days has an error.

+ (Int) getDaysFromAnotherDay :( longlong) date {// date is the timestamp

// Return [self getDaysFrom1970: date]-[self getDaysFrom1970: [NSDate date] timeIntervalSince1970];

NSDateComponents * comps = [[NSDateComponentsalloc] init];

[CompssetDay: [NSDateUtilsgetDay: date];

[CompssetMonth: [NSDateUtilsgetMonth: date];

[CompssetYear: [NSDateUtilsgetYear: date];

NSCalendar * gregorian = [[NSCalendaralloc] initWithCalendarIdentifier: NSGregorianCalendar];

NSDate * endDate = [[NSDatealloc] init];

NSDate * startDate = [gregoriandateFromComponents: comps];

[Compsrelease];

Unsignedint unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit;

NSDateComponents * comps2 = [gregoriancomponents: unitFlags fromDate: startDatetoDate: endDate options: 0];

Int days = [comps2day];

Return days;

}


+ (Int) getYear :( longlong) date {

Return [[selfgetNSDateComponents: date] year];

}


+ (Int) getMonth :( longlong) date {

Return [[selfgetNSDateComponents: date] month];

}


+ (Int) getDay :( longlong) date {

Return [[selfgetNSDateComponents: date] day];

}


+ (NSDateComponents *) getNSDateComponents :( longlong) date {

NSInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;

NSDateComponents * comps = [[selfshareNSCalendar] components: unitFlags fromDate: [NSDatedateWithTimeIntervalSince1970: date];

Return comps;

}



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.