The NSDate class is used to hold time values, and it provides methods to handle some of the day-to-night comparisons between the seconds-based difference (temporal Interval) operations and dates.
1. The following methods are available for creating or initializing
used to create The class method for 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)
used to create instance methods for NSDate instances are
-(ID) Addtimeinterval: (nstimeinterval) secs;
Returns the time that is saved in the current instance, and then secs seconds
used to initialize instance methods for NSDate instances are
-(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"
At the same time, a more commonly used method to obtain the number of milliseconds since 1970:
Objective-c Code
- Nstimeinterval time = [[NSDate date] timeIntervalSince1970];
- Nstimeinterval returns a double type, and the output is displayed as a decimal integer plus some other values
- If you want to turn int, you must turn into a long long to be large enough.
- Nstimeinterval time = [[NSDate date] timeIntervalSince1970];
- Long Long dTime = [[NSNumber numberwithdouble:time] longlongvalue]; Convert double to long long type
- NSString *curtime = [NSString stringwithformat:@"%llu", dTime];//output long long type
NSDate and get the current time and other common operations