The string is converted to the NSDate type, which calculates the difference from the current time, the number of years, the number of days, and seconds

Source: Internet
Author: User

NSString *datestr=@"2013-08-13 20:28:40";//Incoming Time//Convert incoming time into desired formatNSDateFormatter *format=[[NSDateFormatter alloc] init]; [Format Setdateformat:@"YYYY-MM-DD HH:mm:ss"]; NSDate*fromdate=[format DATEFROMSTRING:DATESTR]; Nstimezone*fromzone =[Nstimezone Systemtimezone]; Nsinteger Frominterval=[Fromzone secondsfromgmtfordate:fromdate]; NSDate*fromdate =[FromDate Datebyaddingtimeinterval:frominterval]; NSLog (@"fromdate=%@", FromDate); [Format release];//Get current TimeNSDate *date =[NSDate Date]; Nstimezone*zone =[Nstimezone Systemtimezone]; Nsinteger interval=[Zone secondsfromgmtfordate:date]; NSDate*localedate =[Date Datebyaddingtimeinterval:interval]; NSLog (@"enddate=%@", localedate); DoubleIntervalTime = [FromDate timeintervalsincereferencedate]-[Localedate timeintervalsincereferencedate]; LongLtime = (Long) IntervalTime; Nsinteger Iseconds= ltime% -; Nsinteger iminutes= (Ltime/ -) % -; Nsinteger ihours= (Ltime/3600)$; Nsinteger idays= ltime/ -/ -/ -; Nsinteger Imonth= ltime/ -/ -/ -/ A; Nsinteger Iyears= ltime/ -/ -/ -/384; NSLog (@"M-year D-month or D-day D-minute D-Second", iyears,imonth,idays,ihours,iminutes,iseconds);//The code snippet comes from:http://www.sharejs.com/codes/objectc/5099

The string is converted to the NSDate type, which calculates the difference from the current time, the number of years, the number of days, and seconds

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.