Nsdatepicker && NSDate

Source: Internet
Author: User

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

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 current (now), you can call the method. The test returned 0001/12/31 17:00:00 BC

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

-(ID) initwithtimeintervalsincereferencedate: (nstimeinterval) secs;

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 minutes of the existence of GMT. For example, if the time zone is set in Beijing, then "±HHMM" is displayed as "+0800"

Get the current date

NSDate *date = [NSDate Date];

NSLog (@ "date:%@", date);

Get (24 * 60 * 60) that is 24 hours before the date, Datewithtimeintervalsincenow:

NSDate *yesterday = [NSDate datewithtimeintervalsincenow:-(24 * 60 * 60)];

NSLog (@ "yesterday:%@", yesterday);

NSDateFormatter *formatter =[[[nsdateformatter alloc] init] autorelease];

NSDate *date = [NSDate Date];

[Formatter Settimestyle:nsdateformattermediumstyle];

Nscalendar *calendar = [[[Nscalendar alloc] Initwithcalendaridentifier:nsgregoriancalendar] autorelease];

Nsdatecomponents *comps = [[[[Nsdatecomponents alloc] init] autorelease];

Nsinteger unitflags = Nsyearcalendarunit |

Nsmonthcalendarunit |

Nsdaycalendarunit |

Nsweekdaycalendarunit |

Nshourcalendarunit |

Nsminutecalendarunit |

Nssecondcalendarunit;

int week=0;

Comps = [Calendar Components:unitflags fromdate:date];

Int week = [comps weekday];

int year=[comps year];

int month = [Comps month];

int day = [Comps day];

[Formatter Setdatestyle:nsdateformattermediumstyle];

This sets the label with the updated time.

int hour = [Comps hour];

int min = [comps minute];

int sec = [comps second];

NSLog (@ "week%d", week);

NSLog (@ "year%d", year);

NSLog (@ "month%d", month);

NSLog (@ "day%d", day);

NSLog (@ "hour%d", hour);

NSLog (@ "min%d", min);

NSLog (@ "sec%d", sec);

Get milliseconds

NSDateFormatter *dateformatter = [[NSDateFormatter alloc] init];

[Dateformatter Setdatestyle:nsdateformattermediumstyle];

[Dateformatter Settimestyle:nsdateformattershortstyle];

[Dateformatter setdateformat:@ "Hh:mm:ss"]

[Dateformatter setdateformat:@] Yyyy-mm-dd HH:mm:ss. SSS "];

NSLog (@ "date%@", [Dateformatter stringfromdate:[nsdate Date]]);

[Dateformatter release];

Nsdatepicker && NSDate

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.