Today summarizes some of the nsdate in OC simple usage, attention is not NSData Oh!
Use of nsdate
1> NSDate Initialization method
Get current time
NSDate * date = [NSDate date];
Returns the current time base, and then the time after secs seconds
[NSDate datewithtimeintervalsincenow:60 * 60 * 24];
Returns the Lastdate as the benchmark, then secs seconds later
[NSDate datewithtimeinterval:60 * sincedate:lastdate];
The return is based on January 1, 2001, and then secs seconds later.
[NSDate datewithtimeintervalsincereferencedate:0];
The return is based on January 1, 1970, and then secs seconds later.
[NSDate datewithtimeintervalsince1970:60 * 60 * 24];
2> Get time limit method
The time limit of the future 4000
[NSDate Distantfuture];
Past Time limits 00
[NSDate Distantpast];
3> Comparison Time method
Compare two times if they are the same
[Date Isequaltodate:date2];
Returns a date that is relatively late
[Date Laterdate:date2];
Returns the earlier date
[Date Earlierdate:date2];
Returns the Nscomparisonresult object
[Date Compare:date2];
4> Get time interval method
Get a two time interval
[Date2 timeintervalsincedate:date];
Get the time interval between now and 2001
[NSDate timeintervalsincereferencedate];
Get Date1 and 2001 time interval
[Date1 Timeintervalsincereferencedate];
Get date1 and current time interval
Date1.timeintervalsincenow
5> Add time interval
[Date datebyaddingtimeinterval:60 * 60 * 24];
6> display dates based on the current position string
[Date Descriptionwithlocale:[nslocale Currentlocale]];
2. NSDateFormatter formatted Date class
Example:
NSDate * date = [NSDate date];
Initialization
NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
Set Date format
Formatter.datestyle = Nsdateformatterfullstyle;
Set the time format
Formatter.timestyle = Nsdateformattershortstyle;
Setting the regional language
Formatter.locale = [Nslocale localewithlocaleidentifier:@ "ZH-CN"];
Convert dates to strings in the format above
NSString * datestring = [formatter stringfromdate:date];
NSLog (@ "%@", datestring);
Custom date format NSDate goto nstring:
NSDate * date = [NSDate date];
NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
Set Custom date formats
Formatter.dateformat = @ "Yyyy-mm-dd a hh:mm:ss. SSS E Z ";
NSString * datestring = [formatter stringfromdate:date];
NSLog (@ "%@", datestring);
Custom date format nstring goto nsdate:
NSString * string = @ "2015-12-29 10:18:19";
NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
Formatter.dateformat = @ "Yyyy-mm-dd HH:mm:ss";
NSDate * date = [formatter datefromstring:string];
NSLog (@ "%@", date);
Summary DateFormat Format:
YYYY year
MM Month
DD Day
HH 24 hrs
HH 12 hrs
MM min
SS sec
SSS microseconds
A morning afternoon
E-week
Z Time Zone
3. Application of Time Class
1, test the execution time of a piece of code
NSDate * date = [NSDate date];
Put the test code here.
NSLog (@ "%LF", [[NSDate Date] timeintervalsincedate:date]);
2. Unix Timestamp
NSDate turn Timestamp
[[NSDate Date] timeIntervalSince1970];
Time-stamped turn nsdate
[NSDate datewithtimeintervalsince1970:1451394269];
iOS Development-OC Chapter-nsdate