Recent projects have similar QQ space display dynamic UI, imitation of the QQ space time display, in this record, for review.
This is the UI for QQ space:
The time is displayed as:
1. Today--Today xx:xx ( today 15:39)
2. Yesterday-Yesterday xx:xx ( yesterday)
3. Day before yesterday Xx:xx ( 19:00day before yesterday)
4. Same year, for example: January 3-->01-03 xx:xx (01-03 12:29) of the same year
5. Not in the same year-xxxx-xx-xx (2014-12-12)
The program works as follows, with the following simple tests, no UI interface:
The code below is changed on the basis of the existing code on the Web, the original code point this view
/** * Imitation QQ space Time display * @param string eg:2015 May 24 02:21 30 seconds */-(NSString *) format: (NSString *) string{nsdateformatter *i nputformatter= [[NSDateFormatter alloc] init]; [Inputformatter Setlocale:[[nslocale alloc] initwithlocaleidentifier:@ "en_US"]; [Inputformatter setdateformat:@ "yyyy years mm DD Day hh mm min ss seconds"]; Nsdate*inputdate = [Inputformatter datefromstring:string]; NSLog (@ "startdate=%@", inputdate); NSDateFormatter *outputformatter= [[NSDateFormatter alloc] init]; [Outputformatter Setlocale:[nslocale Currentlocale]; [Outputformatter setdateformat:@ "Yyyy-mm-dd HH:mm:ss"]; Get date str nsstring *str= [Outputformatter stringfromdate:inputdate]; Str to nsdate nsdate *strdate = [Outputformatter datefromstring:str]; Fixed 8-hour difference when nstimezone *zone = [Nstimezone systemtimezone]; Nsinteger interval = [zone secondsfromgmtfordate:strdate]; NSDate *enddate = [Strdate datebyaddingtimeinterval:interval]; NSLog (@ "enddate:%@", endDate); NsString *lasttime = [self comparedate:enddate]; NSLog (@ "lasttime =%@", lasttime); return str;} -(NSString *) Comparedate: (NSDate *) date{nstimeinterval secondsperday = 24 * 60 * 60; Fixed 8-hour difference nsdate *date1 = [NSDate Date]; Nstimezone *zone = [Nstimezone systemtimezone]; Nsinteger interval = [zone secondsfromgmtfordate:date1]; NSDate *localedate = [Date1 datebyaddingtimeinterval:interval]; NSLog (@ "nowdate=%@\nolddate =%@", localedate,date); NSDate *today = localedate; NSDate *yesterday,*beforeofyesterday; This year nsstring *toyears; Toyears = [[Today description] substringtoindex:4]; Yesterday = [Today datebyaddingtimeinterval:-secondsperday]; Beforeofyesterday = [Yesterday Datebyaddingtimeinterval:-secondsperday]; Ten first characters of description is the calendar date:nsstring *todaystring = [[Today description] Substringtoind EX:10]; NSString *yesterdaystring = [[Yesterday description] SubstringtoindeX:10]; NSString *beforeofyesterdaystring = [[beforeofyesterday description] substringtoindex:10]; NSString *datestring = [[date description] substringtoindex:10]; NSString *dateyears = [[date description] substringtoindex:4]; NSString *datecontent; if ([Dateyears Isequaltostring:toyears]) {///the same year//the time of the day before yesterday nsstring *time = [[Date description] Substringwi Thrange: (nsrange) {11,5}]; Other time NSString *time2 = [[Date description] Substringwithrange: (nsrange) {5,11}]; if ([datestring isequaltostring:todaystring]) {datecontent = [NSString stringwithformat:@ "Today%@", time]; return datecontent; } else if ([datestring isequaltostring:yesterdaystring]) {datecontent = [NSString stringwithformat:@ "Yesterday%@", Tim E]; return datecontent; }else if ([datestring isequaltostring:beforeofyesterdaystring]) {datecontent = [NSString stringwithformat:@] the day before yesterday %@ ", Time]; Return Datecontent }else{return time2; }}else{return datestring; }}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
iOS date to today yesterday form