Common nsdate usage
1.The following methods are available for creation or initialization:
The class methods used to create an nsdate instance include:
+ (ID) date;
Returns the current time.
+ (ID) datewithtimeintervalsincenow :( nstimeinterval) secs;
The return value is based on the current time, and the time after secs seconds is passed.
+ (ID) datewithtimeintervalsincereferencedate :( nstimeinterval) secs;
The returned time is based on 2001/01/01 GMT, and the time passed by secs seconds.
+ (ID) datewithtimeintervalsince1970 :( nstimeinterval) secs;
The return value is based on GMT, and the time after secs seconds is passed.
+ (ID) distantfuture;
Returns the day of the future many years later.
For example, if you need a time value that is later than the current time (now), you can call this method. The test returns 4000/12/31 16:00:00
+ (ID) distantpast;
Returns a day many years ago.
For example, if you need a time value that is earlier (smaller) than the current time, you can call this method. Test returned 0001/12/31 17:00:00 BC
The method used to create an nsdate instance is
-(ID) addtimeinterval :( nstimeinterval) secs;
The returned result is based on the time saved in the current instance, and the time passed by secs seconds
The methods used to initialize an nsdate instance include:
-(ID) Init;
Initialize to the current time. Similar to the date Method
-(ID) initwithtimeintervalsincereferencedate :( nstimeinterval) secs;
The initialization is based on 2001/01/01 GMT, and the time after secs seconds is passed. Similar to datewithtimeintervalsincereferencedate: Method
-(ID) initwithtimeinterval :( nstimeinterval) secs sincedate :( nsdate *) refdate;
Initialized to refdate as the benchmark, and then the time passed by secs seconds
-(ID) initwithtimeintervalsincenow :( nstimeinterval) secs;
The initialization is based on the current time, and the time after secs seconds is passed.
2.The following methods are available for comparison between dates:
-(Bool) is1_todate :( nsdate *) otherdate;
Yes is returned for comparison with otherdate.
-(Nsdate *) earlierdate :( nsdate *) anotherdate;
Returns the date earlier than anotherdate.
-(Nsdate *) laterdate :( nsdate *) anotherdate;
Returns the date later than anotherdate.
-(Nscomparisonresult) Compare :( nsdate *) Other;
This method is called for sorting:
. Nsorderedsame is returned when the date value saved by the instance and anotherdate are the same.
. Nsordereddescending is returned when the date value saved by the instance is later than anotherdate.
. Nsorderedascending is returned when the date value saved by the instance is earlier than anotherdate.
3.You can use the following methods to retrieve the time interval:
-(Nstimeinterval) timeintervalsincedate :( nsdate *) refdate;
Returns the time interval between the Instance Storage Time and refdate Based on refdate.
-(Nstimeinterval) timeintervalsincenow;
Returns the time interval between the Instance Storage Time and the current time (now) based on the current time (now ).
-(Nstimeinterval) timeintervalsince1970;
Return the time interval between the Instance Storage Time and GMT based on January 1, GMT.
-(Nstimeinterval) timeintervalsincereferencedate;
Returns the time interval between the Instance Storage Time and the GMT value of 2001/01/01.
+ (Nstimeinterval) timeintervalsincereferencedate;
Returns the interval between the current time (now) and 2001/01/01 GMT Based on the GMT value of 2001/01/01.
4.Represents time as a string
-(Nsstring *) description;
Time is represented in the format of YYYY-MM-DD hh: mm: SS ± hhmm.
"± Hhmm" indicates the number of hours and minutes of time zone difference with GMT. For example, if the time zone is set to Beijing, "± hhmm" is displayed as "+ 0800"