The number of seconds to take the current time
Nstimeinterval time = [[NSDate date] timeIntervalSince1970];
A long long int date = (long long int) time;
NSLog (@ "date\n%d", date); 1295322949
Convert the number of seconds into YYYY-MM-DD hh:mm:ss format
NSDate *DD = [NSDate datewithtimeintervalsince1970:date];
NSLog (@ "d:%@", DD); 2011-01-18 03:55:49 +0000
Give a time in seconds, take out the corresponding time
NSString *s = @ "1295355600000″; Correspondence 21:00
NSDate *d = [NSDate datewithtimeintervalsince1970:[s doublevalue]/1000];
NSLog (@ "dddd:%@", D); 2011-01-18 13:00:00 +0000
NSDateFormatter *formatter1 = [[NSDateFormatter alloc] init];
[Formatter1 setdateformat:@ "hh:mm"];
NSString *showtimenew = [Formatter1 stringfromdate:d];
NSLog (@ "showtimenew:%@", showtimenew); 21:00 than D time + 8 hours
[Formatter1 release];
1. The following methods are available for creating or initializing
The class method used to create the NSDate instance has
+ (ID) date;
Return Current time
+ (ID) Datewithtimeintervalsincenow: (nstimeinterval) secs;
Returns the current time base, and then the time after secs seconds
+ (ID) datewithtimeintervalsincereferencedate: (nstimeinterval) secs;
Returns the 2001/01/01 GMT as the benchmark, then secs seconds later
+ (ID) dateWithTimeIntervalSince1970: (nstimeinterval) secs;
Returns the 1970/01/01 GMT as the benchmark, then secs seconds later
+ (ID) distantfuture;
Return a certain day of the future many years later. (For example, if you need a time value that is longer than the current (now) night, you can call the method.) The test returned 4000/12/31 16:00:00)
+ (ID) distantpast;
Return a day many years ago. (For example, if you need a time value that is much longer than the present (now), you can call the method.) Test returned BC 0001/12/31 17:00:00)
The instance method used to create the NSDate instance has
-(ID) Addtimeinterval: (nstimeinterval) secs;
Returns the time that is saved in the current instance, and then secs seconds
The instance method used to initialize the NSDate instance has
-(ID) init;
Initialized to the current time. Similar to the Date method
Initialized to 2001/01/01 GMT, and then secs seconds later. Similar Datewithtimeintervalsincereferencedate: Method
-(ID) Initwithtimeinterval: (nstimeinterval) secs sincedate: (NSDate *) refdate;
Initialized to Refdate, then secs seconds later.
-(ID) Initwithtimeintervalsincenow: (nstimeinterval) secs;
Initialized to the current time, and then secs seconds later
2. Comparisons between dates are available in the following ways
-(BOOL) Isequaltodate: (NSDate *) otherdate;
Same as Otherdate, return Yes
-(NSDate *) Earlierdate: (NSDate *) anotherdate;
Returns the earlier date compared to the Anotherdate
-(NSDate *) Laterdate: (NSDate *) anotherdate;
Compare with Anotherdate, return to the later date
-(Nscomparisonresult) Compare: (NSDate *) Other;
Called when the method is used for sorting:
. When the date value saved by the instance is returned with the anotherdate phase Nsorderedsame
. Returns nsordereddescending when the instance holds a date value that is later than anotherdate
. Returns nsorderedascending when the instance holds a date value earlier than anotherdate
3. The time interval can be retrieved using the following methods
-(Nstimeinterval) Timeintervalsincedate: (NSDate *) refdate;
Returns the time interval between the time the instance was saved and the Refdate, with Refdate as the base time
-(Nstimeinterval) Timeintervalsincenow;
Returns the time between the time that the instance was saved and the current time (now) at the current time (now)
-(Nstimeinterval) timeIntervalSince1970;
Returns the time between the time the instance was saved and 1970/01/01 GMT, with 1970/01/01 GMT as the base time
-(Nstimeinterval) timeintervalsincereferencedate;
Returns the time between the time the instance was saved and 2001/01/01 GMT, with 2001/01/01 GMT as the base time
+ (Nstimeinterval) timeintervalsincereferencedate;
Returns the time interval between the current time (now) and 2001/01/01 GMT, with 2001/01/01 GMT as the base time
4. Representing time as a string
-(NSString *) description;
The time is expressed in YYYY-MM-DD hh:mm:ss±hhmm format. (where "±hhmm" represents the time zone difference between how many hours and how long the GMT exists.) For example, if the time zone is set in Beijing, then "±HHMM" is displayed as "+0800″"
Use of Ios-nssdate