Where did all the time go?

Source: Internet
Author: User
Tags date1

A recent project involves time issues, especially the current time and the time stamp obtained to convert, sending the current timestamp to the server. When the interface is displayed, it shows the minutes and seconds. In this way, all kinds of calculation solution days, hours, minutes, seconds, although simple, but the real writing is still a lot of convenience, so I encapsulated a small method, sharing: "Personal use, if there is a more optimized method, but also look at the liberal enlighten" words do not say more code:

-(NSString *) Timedifference: (NSString *) TimeStamp {//converting timestamps to stringsNSDate*confromtimesp =[NSDate Datewithtimeintervalsince1970:[timestamp intvalue]; Nstimezone* Zone =[Nstimezone Systemtimezone]; Nsinteger interval=[Zone SECONDSFROMGMTFORDATE:CONFROMTIMESP]; NSDate*date1 =[Confromtimesp Datebyaddingtimeinterval:interval]; NSDate* Datenow =[self getcurrenttime]; NSLog (@"%@ corresponding to time stamp", date1); NSLog (@"the current time corresponds to the%@", Datenow); Nstimeinterval Time=[Datenow timeintervalsincedate:date1]; intDays= ((int) time)/(3600* -); intHours= ((int)% (3600* -)/3600; intMinute= ((int)% (3600* -)/ --hours* -; intSecond = (int) time-minute* --hours*3600-days* -*3600; NSString*datecontent=[[nsstring Alloc] Initwithformat:@"%i days%i hours%i minutes%i seconds", Days,hours,minute,second]; //NSString * differencetime = [NSString stringwithformat:@ "%i minutes%i seconds", Minute,second];NSLog (@"Time difference between dates:%@", datecontent); returndatecontent; }

This method is to get a timestamp, the time stamp and the current time to make a difference, get the time interval, the return value is more casual, I have all returned, as necessary to delete it. Sending the current timestamp is also straightforward, with a straight line of code:

      NSDate * sendate = [NSDate date];           * Date2  = [NSString stringWithFormat:@ "%ld", (long) [Sendate  TIMEINTERVALSINCE1970]];          NSLog (@ " current timestamp:%@", Date2);    

Where did all the time go?

Related Article

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.