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?