ios-Time Conversion

Source: Internet
Author: User

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

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.