NSDate Common methods
/*=============================nsdate use of the date class =============================*/ //get the current time, get 0 City time is 8 hours difference from Beijing timeNSDate *currentdate = [NSDate Date];//GMTNSLog (@"currentdate:%@", currentdate); //timeIntervalSince1970 to 1970-1-1 seconds, also called timestamp (nstimeinterval)Nstimeinterval interval1970 =[currentdate timeIntervalSince1970]; NSLog (@"interval =%lf", interval1970); //timeintervalsincereferencedate in seconds to 2001-1-1Nstimeinterval interval2001 =[currentdate timeintervalsincereferencedate]; NSLog (@"interval2001 =%lf", interval2001); //Timeintervalsincenow the number of seconds from the current timeNstimeinterval Intervalnow =[currentdate Timeintervalsincenow]; NSLog (@"Intervalnow =%lf", Intervalnow); Nstimeinterval Hour= -* -; //an hour laterNSDate *H1 =[currentdate Datebyaddingtimeinterval:hour]; NSLog (@"H1:%@", H1); //an hour ago .NSDate *H2 = [currentdate datebyaddingtimeinterval:-Hour]; NSLog (@"H2:%@", H2); //calculate time in Beijing timezone (using the current time zone of the system: Systemtimezone)Nstimeinterval inter =[[Nstimezone Systemtimezone] secondsfromgmt]; NSDate*bjdate =[currentdate Datebyaddingtimeinterval:inter]; NSLog (@"bjdate:%@", bjdate); Nstimeinterval Day= -* -* -; //Way One://nsdate *yesterday = [NSDate datewithtimeintervalsincenow:-day]; //Way Two://YesterdayNSDate *yesterday = [Bjdate datebyaddingtimeinterval:-Day ]; NSLog (@"yesterday:%@", yesterday); //TodayNSDate *today = [Bjdate datebyaddingtimeinterval:0]; NSLog (@"today:%@", today); //TomorrowNSDate *tomorrow =[Bjdate Datebyaddingtimeinterval:day]; NSLog (@"tomorrow:%@", tomorrow); //isequaltodate Comparison of two dates if([Yesterday Isequaltodate:tomorrow]) {NSLog (@"Two dates same"); } Else{NSLog (@"Two dates are different"); } //Compare comparison of two datesNscomparisonresult result =[Yesterday Compare:tomorrow]; if(Result = =nsorderedascending) {NSLog (@"Date Ascending"); } Else if(Result = =nsorderedsame) {NSLog (@"Two dates same"); } Else if(Result = =nsordereddescending) {NSLog (@"Two date Descending"); } //Distantfuture A time of the future 4001-01-01 00:00:00NSDate *future =[NSDate distantfuture]; NSLog (@"Future :%@", future); //Distantpast A time of ancient 0001-12-30 00:00:00NSDate *past =[NSDate Distantpast]; NSLog (@"Past:%@", past); //dateWithTimeIntervalSince1970 converting timestamps to date typesNSString *time =@"34567898"; NSDate*timedate =[NSDate datewithtimeintervalsince1970:[time doublevalue]; NSLog (@"timedate:%@", timedate); NSDate*now =[NSDate Date]; NSDateFormatter*formatter =[[NSDateFormatter alloc] init]; [Formatter Setdateformat:@"yyyy mm month DD Day HH:MM:SS"]; //October 15, 2014 16:35:42//stringfromdate Format a date type to NSString typeNSString *current =[Formatter Stringfromdate:now]; NSLog (@"current:%@", current); //datefromstring Converts a time string to a date type,[Formatter Settimezone:[nstimezone TIMEZONEFORSECONDSFROMGMT:0]];//0 Time zone is used by default, so time zone conversion is requiredNSDate *nowdate =[Formatter datefromstring:current]; NSLog (@"nowdate:%@", nowdate);
This article GitHub address: Https://github.com/zhangkiwi/iOS_SN_NSDate
Common methods of ios-oc-Foundation-nsdate