Common usage of iOS Study Notes 25-NSDate (1)

Source: Internet
Author: User

 

1. The following methods can be used to create or Initialize an instance:

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. The interval for retrieving data can be as follows:

-(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. Represent the 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"

 


 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.