Often encounter time conversion, in this collection of a time conversion method?
1 #pragmaMark Time Conversion2 3+ (NSString *) Setcreatetime: (NSString *) Str4 {5 //Yyyy-mm-dd-hh:mmss "];6 7 8nsdateformatter* formatter =[[NSDateFormatter alloc] init];9 Ten //[Formatter setdatestyle:nsdateformattermediumstyle]; One A //[Formatter settimestyle:nsdateformattershortstyle]; - -[Formatter Setdateformat:@"yyyy"]; the -[Formatter Settimezone:[nstimezone timezonewithabbreviation:@"CCT"]]; - -NSDate *date =[NSDate datewithtimeintervalsince1970:[str doublevalue]; + -NSString *datestr =[Formatter stringfromdate:date]; + A //local time atNSDate * senddate=[NSDate Date]; -NSDateFormatter * dateformatter=[[NSDateFormatter alloc] init]; -[Dateformatter Setdateformat:@"YYYY"]; -NSString * locationstring=[Dateformatter stringfromdate:senddate]; - - //[dateformatter setdateformat:@ "Yyyy-mm-dd-hh-mm-ss"]; in - //determine whether the creation time and local time are the same year to if([datestr isequaltostring:locationstring]) { +[Formatter Setdateformat:@"MM-DD hh:mm"]; -}Else{ the[Formatter Setdateformat:@"YYYY-MM-DD hh:mm"]; * } $ Panax NotoginsengDate =[NSDate datewithtimeintervalsince1970:[str doublevalue]; -Datestr =[Formatter stringfromdate:date]; the + return[self comparecurrenttime:date:d atestr]; A } the ++ (NSString *) Comparecurrenttime: (nsdate*) comparedate:(NSString *) Str - { $Nstimeinterval timeinterval =[Comparedate Timeintervalsincenow]; $TimeInterval =-timeinterval; - Longtemp =0; -NSString *result; the if(TimeInterval < -) { - if(TimeInterval >1) {Wuyiresult = [NSString stringWithFormat:@"%d seconds ago",(int) timeinterval+1]; the}Else if(TimeInterval <- the){ -result =str; Wu } - Else{ Aboutresult = [NSString stringWithFormat:@"%d seconds ago",1]; $ } - - } - Else if(temp = timeinterval/ -) < -){ Aresult = [NSString stringWithFormat:@"%ld minutes ago", temp]; +}Else{ theresult =str; - } $ the the the returnresult; the}View Code
ios-Time Conversion