1, read the current date format, and then automatically convert to yyyy-mm-dd HH:mm:ss 0000
Date formatter NSDateFormatter *fmt = [[NSDateFormatter alloc] init]; E: Days of the Week//m: Month//d: Number (the day of the Month)//H: 24-hour hour//m: minutes//S: sec//Y: Year//_created_at = Thu Oct 16 17 : 06:25 +0800 2014//Read the current date format and then automatically convert to yyyy-mm-dd HH:mm:ss 0000 Fmt.dateformat = @ "EEE MMM dd HH:mm:ss Z yyyy";
2, if it is a real machine debugging, convert this European and American time, need to set locale
Fmt.locale = [[Nslocale alloc] initwithlocaleidentifier:@ "en_US"];
3, String Mutual transfer date
String converted to date nsdate *creatdate = [FMT datefromstring:_created_at]; Date converted to string nsstring *str = [FMT stringfromdata:_created_at];
4, compare the difference of two dates
Calendar object Nscalendar *calendar = [Nscalendar Currentcalendar]; Calculates the difference between two dates, the Nscalendarunit enumeration represents what difference you want to get nscalendarunit unit = Kcfcalendarunityear | Kcfcalendarunitmonth | Kcfcalendarunitday | Kcfcalendarunithour | Kcfcalendarunitminute; Get the Contrast object nsdatecomponents *cmps = [Calendar components:unit fromdate:creatdate Todate:now options:0];
5, judging whether it is today, yesterday, this year
- (BOOL) Isthisyear: (nsdate *) creatdate{#warning //take out the current date--day of month nscalendar *calendar = [nscalendar currentcalendar]; Nsdatecomponents *datecmps = [calendar components:nscalendarunityear fromdate: Creatdate]; nsdatecomponents *nowcmps = [calendar components: Nscalendarunityear fromdate:[nsdate date]]; return datecmps.year == nowcmps.year;} - (BOOL) IsToday: (nsdate *) creatdate{ /** another method */ nsdateformatter *fmt = [[nsdateformatter alloc] init]; fmt.dateformat = @ "YYYY-MM-DD"; nsdate *now = [NSDate date]; NSString *nowStr = [fmt Stringfromdate:now]; nsstring *datestr = [fmt stringfromdate:creatdate];      RETURN [NOWSTR ISEQUALTOSTRING:DATESTR];} - (BOOL) Isyesterday: (nsdate *) creatdate{ nsdateformatter *fmt = [[nsdateformatter alloc] init]; fmt.dateformat = @ "DD"; NSDate *now = [NSDate date]; nsstring *nowstr = [fmt stringfromdate:now]; nsstring * Datestr = [fmt stringfromdate:creatdate]; bool isSure = NO; if (Nowstr.intvalue - datestr.intvalue  == 1) { isSure = YES; } //wblog (@ "%d", XX); &NBSp; return issure;}
6, Weibo time display
1, United States time to China time//2, to various modes display-(nsstring *) created_at{ _created_at = @ "Thu aug 26 10:35:34 +0800 2015"; // Date Formatter NSDateFormatter *fmt = [[NSDateFormatter alloc] init]; // e: Day of the week // m: Month // d: The number (the day of the month) // h:24 hour // m: Minute // s: sec // y: Year // _ created_at == thu oct 16 17:06:25 +0800 2014 // Reads the current date format and then automatically converts it to yyyy-mm-dd hh:mm:ss 0000 fmt.dateformat = @ "EEE mmm dd hh:mm:ss z yyyy "; #warning if it is a real machine debug, convert this European and American time, need to set locale fmt.locale =  [[nslocale alloc] initwithlocaleidentifier:@ "en_US"]; //Weibo date of creation nsdate *creatdate = [fmt datefromstring:_created_at]; / /Current Time nsdate *now = [nsdate date]; // Calendar objects (easy to compare before two dates) NSCalendar *calendar = [NSCalendar currentcalendar]; //calculates the difference between two dates, and the Nscalendarunit enumeration represents what difference you want to get nscalendarunit unit = kcfcalendarunityear | kcfcalendarunitmonth | kCFCalendarUnitDay | kCFCalendarUnitHour | kCFCalendarUnitMinute; nsdatecomponents *cmps = [calendar components:unit fromdate:creatdate toDate:now options:0]; if ([self Isthisyear:creatdate]) { if ([self isyesterday:creatdate]) { fmt.dateformat = @ "Yesterday hh:mm"; return [fmt stringFromDate:creatDate]; }else if ([self istoday:creatdate]) { if (cmps.hour > 1) { return [ nsstring stringwithformat:@ "%d hours ago",cmps.hour]; }else if (cmps.minute > 1) { return [nsstring stringwithformat:@ "%d minutes ago", Cmps.minute]; }else{ return @ "just"; } }else{//other days this year fmt.dateformat = @ "MM-dd HH:mm"; return [fmt stringfromdate: creatdate]; } }else{//not this year fmt.dateformat = @ "yyyy-mm-dd hh:mm"; return [fmt stringFromDate:creatDate]; } return _created_at;} - (BOOL) Isthisyear: (nsdate *) creatdate{#warning //take out the current date--year month day NSCalendar *calendar = [NSCalendar currentCalendar]; NSDateComponents *dateCmps = [calendar components:NSCalendarUnitYear fromdate:creatdate]; nsdatecomponents *nowcmps = [calendar components:nscalendarunityear fromdate:[nsdate date]]; return datecmps.year == nowcmps.year;} - (BOOL) IsToday: (nsdate *) creatdate{ /** another method */ nsdateformatter *fmt = [[nsdateformatter alloc] init]; fmt.dateformat = @ "YYYY-MM-DD"; nsdate *now = [NSDate date]; NSString *nowStr = [fmt Stringfromdate:now]; nsstring *datestr = [fmt stringfromdaTe:creatdate]; return [nowstr isequaltostring:datestr ];} - (BOOL) Isyesterday: (nsdate *) creatdate{ nsdateformatter *fmt = [[nsdateformatter alloc] init]; fmt.dateformat = @ "DD"; NSDate *now = [NSDate date]; nsstring *nowstr = [fmt stringfromdate:now]; nsstring * Datestr = [fmt stringfromdate:creatdate]; bool isSure = NO; if (Nowstr.intvalue - datestr.intvalue  == 1) { isSure = YES; } //wblog (@ "%d", XX); return issure;}
ios--Date