NSDate classes that define time
NSDate is a time class that we seldom encounter when writing programs. Today, I suddenly met, feeling very unfamiliar.
To everyone to send a blog, let everyone also review, haha!
Brother use when suddenly found that there are some bugs, we can write to verify the use of.
1, definition: nsdate *date =[nsdate date];//Current time on the addition and subtraction of time
2, action on time , &NB Sp &NBSP
nsdate *date1 = [NSDate datewithtimeintervalsincenow:3600.0*10];// From the current time, a few seconds later, the parameter represents seconds &N Bsp , &NB Sp , &NB Sp
nsdate *date3 = [NSDate datewithtimeintervalsince1970:1000];//from 1970 onwards, after a few seconds
3, calculate the time interval
Nstimeinterval interval = [date timeintervalsince1970];//the number of seconds from 1970 to a certain time
Nstimeinterval interval1 = [Date1 timeintervalsincedate:date];//calculates the interval of two time (s)
Nstimeinterval Interval2 = [date1 timeintervalsincenow];//Gets the interval between a time and the current time
NSLog (@ "%lf", Interval1);
4. Time Comparison
NSDate *date4 = [Date laterdate:date1];//gets two later (longer than 1970 years)
NSDate *date5 = [date earlierdate:date1];
Dateformatter Time Format
5. Conversion between strings and time
NSDateFormatter *formater = [[NSDateFormatter alloc] init];
[Formater setdateformat:@ "Yyyy-mm-dd h:mm:ss"];//format the time display, the Formater format used here is exactly the same as the string format, or the conversion fails
NSString *datestr = [formater stringfromdate:date1];//convert Date to String
NSLog (@ "%@", datestr);
NSDateFormatter *formatter1 = [[NSDateFormatter alloc] init];
[Formatter1 setdateformat:@ "yyyy mm month DD Day Hh:mm:ss"];//the Formater format used here is exactly the same as the string format, or the conversion fails
[Formatter1 Settimezone:[nstimezone localtimezone]];//converts a string into a date
NSString *str = @ "November 11, 2015 11:11:11";
NSDate *date6 = [Formatter1 datefromstring:str];
NSLog (@ "%@", Date6);
6. Time Zone
Nstimezone *zone = [Nstimezone systemtimezone];//get system time zone
[Formater settimezone:zone];//Set time zone
Nsinteger TimeOff = [Zone secondsfromgmt];//get the time zone of the system green jet lag
NSLog (@ "%f", timeoff/3600.0);
NSDate *date7=[formatter1 DATEFROMSTRING:STR];
NSDate *date8 = [Date7 Datebyaddingtimeinterval:timeoff];
NSLog (@ "%@", Date8);
7. Eliminate time difference
NSString *[email protected] "November 10, 2015 11:11:11";
NSDateFormatter *formatter=[[nsdateformatter alloc] init];
[Formatter setdateformat:@ "yyyy mm month DD day HH:MM:SS"];
NSDate *date=[formatter DATEFROMSTRING:STR];
NSLog (@ "%@", date);
[Formatter Settimezone:[nstimezone timezonewithname:@ "UTC"];
NSDate *date1=[formatter DATEFROMSTRING:STR];
NSLog (@ "%@", date1);
[Formatter Settimezone:[nstimezone timezoneforsecondsfromgmt:0];
NSDate *date2=[formatter DATEFROMSTRING:STR];
NSLog (@ "%@", date2);
Nsinteger Timeoff=[[nstimezone Localtimezone] secondsfromgmt];
NSDate *date3=[date Datebyaddingtimeinterval:timeoff];
NSLog (@ "%@", date3);
8, the difference between the data
NSDate *data=[[nsdata Alloc]init];
NSString *[email protected] "1234";
NSData *data1=[str datausingencoding:nsutf8stringencoding];//Converts a string to data,nsutf8stringencoding represents the encoding format.
NSString *str1=[[nsstring alloc]initwithdata:data encoding:nsutf8stringencoding];//data Convert to String
NSLog (@ "%@", data1);//Output data type
NSLog (@ "%zi", data1.length);//Output data length
NSData *data2 = [NSData datawithcontentsofurl:[nsurl urlwithstring:@ "Http://www.iconpng.com/png/ios7-premium/tree11 . png "]];//get network data
NSLog (@ "%@", data2);
Nsmutabledata *data3 = [Nsmutabledata data];//Create an empty data
[Data3 appenddata:data];//splicing data
NSLog (@ "%@", data3);
ios-Time Class Date detailed