iOS development-Calculates the time difference, calculates the date, and calculates the age.

Source: Internet
Author: User

Many of the projects I have developed are useful to calculate the time difference, here I have a small package of methods to share to the needs of friends, of course, mainly beginners, haha.

The main use of NSDate, Nscalender, nscomponents three classes, these three classes inside a lot of methods, everyone can browse, especially need to pay attention to the version of the method behind the limitations, note that the use of version adaptation issues. Not much nonsense, direct show you the code:

The first is. h

1 /**2 * Calculate time difference3  *4 * @param startdate start time5 * @param endDate End time6  *7 * @return Time difference8  */9+ (Nsdatecomponents *) calculatetimedifferencewithstartdatestring: (NSString *) startstring endDateString: (NSString *) endstring;Ten+ (Nsdatecomponents *) Calculatetimedifferencewithstartdate: (NSDate *) startdate endDate: (NSDate *) endDate; One+ (Nsdatecomponents *) Calculateagewithdate: (NSDate *) Date; A+ (Nsdatecomponents *) calculateagewithdatestring: (NSString *)string;

And then the. m

1+ (Nsdatecomponents *) calculatetimedifferencewithstartdatestring: (NSString *) startstring endDateString: (NSString *) endstring {2     3Startstring =[self datestringformatter:startstring];4Endstring =[self datestringformatter:endstring];5NSDateFormatter *dateformatter =[[NSDateFormatter alloc] init];6[Dateformatter Setdateformat:@"YYYYMMDDHHMMSS"];7 [Dateformatter settimezone:[nstimezone localtimezone];8     9     return[self calculatetimedifferencewithstartdate:[dateformatter datefromstring:startstring] EndDate:[dateFormatter Datefromstring:endstring]];Ten } One  A+ (Nsdatecomponents *) Calculatetimedifferencewithstartdate: (NSDate *) startdate endDate: (NSDate *) EndDate { -      -     //Calendar theNscalendar *calendar =[Nscalendar Currentcalendar]; -     //Date Comparison Items -Nsuinteger unitflags = Nscalendarunitera | Nscalendarunityear | Nscalendarunitmonth | Nscalendarunitday | Nscalendarunithour | Nscalendarunitminute | Nscalendarunitsecond |Nscalendarunitweekday; -  +     return[Calendar components:unitflags fromdate:startdate todate:enddate options:nscalendarmatchstrictly]; - } +  A+ (Nsdatecomponents *) Calculateagewithdate: (NSDate *) Date { at      -     return[self calculatetimedifferencewithstartdate:date enddate:[nsdate Date]]; - } -  -+ (Nsdatecomponents *) calculateagewithdatestring: (NSString *) datestring { -      inDateString =[self datestringformatter:datestring]; -NSDateFormatter *dateformatter =[[NSDateFormatter alloc] init]; to[Dateformatter Setdateformat:@"YYYYMMDDHHMMSS"]; +[Dateformatter Settimezone:[nstimezone Localtimezone];//Set Date time zone -      the     return[self calculateagewithdate:[dateformatter datefromstring:datestring]]; * } $ Panax Notoginseng /** - * Date String formatting method the  * + * @param datestring Date string A  * the * @return Formatted string +  */ -+ (NSString *) Datestringformatter: (NSString *) datestring { $      $     //go to the symbol of the date string -datestring = [DateString stringbyreplacingoccurrencesofstring:@"-"Withstring:@""]; -datestring = [DateString stringbyreplacingoccurrencesofstring:@":"Withstring:@""]; thedatestring = [DateString stringbyreplacingoccurrencesofstring:@"/"Withstring:@""]; -datestring = [DateString stringbyreplacingoccurrencesofstring:@" "Withstring:@""];Wuyi      the     //time string length exceeded, intercept 14 bits -     if([datestring Length] > -) { Wudatestring = [DateString substringtoindex: -]; -     } About      $     //time string length is not enough, complement 0 -      while([datestring Length] < -) { -datestring = [DateString stringbyappendingstring:@"0"]; -     } A      +     returndatestring; the}

Thanks for watching!

iOS development-Calculates the time difference, calculates the date, and calculates the age.

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.