The nsdate class is used to save time values. It also provides some methods for processing time interval operations and comparison between dates.
1.The following methods are available for creation or initialization:
Used to create nsdateThe instance class methods 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. The test returned at 17:00:00, 0001/12/31, BC)
Used to create nsdateThe instance methods are as follows:
-(ID) addtimeinterval :( nstimeinterval) secs;
The returned result is based on the time saved in the current instance, and the time passed by secs seconds
Used to initialize nsdateThe instance methods are as follows:
-(ID) Init;
Initialize to the current time. Similar to the date Method
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 ")
At the same time, we provide a common method to obtain the number of milliseconds since January 1, 1970:
Objective-C code
- Nstimeinterval time = [[nsdate date] timeintervalsince1970];
- // Nstimeinterval returns the double type. The output is displayed as a 10-digit integer plus a decimal point and some other values.
- // If you want to convert to int type, you must convert to long type to be large enough.
- Nstimeinterval time = [[nsdate date] timeintervalsince1970];
- Long long dtime = [[nsnumber numberwithdouble: Time] longlongvalue]; // convert double to long
- Nsstring * curtime = [nsstring stringwithformat: @ "% LlU", dtime]; // output long