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.