iOS Learning Note---OC language Seventh Day

Source: Internet
Author: User

Extension of the class

NSDate is the base class for processing dates and times in cocoa, encapsulating a given moment, specific datetime, and time zone

Use the +date method to get the current date and time

1NSDate *date = [NSDate Date];//Get current Time2NSLog (@"%@", date);//No matter what time zone you are in, the time to print is always o timezone3NSDate *date2 = [NSDate datewithtimeintervalsincenow: -* -*8];//the time from now onwards4NSLog (@"%@", date2);5Nstimeinterval date3 = [date timeIntervalSince1970];//Get time interval from 1970 to present6NSLog (@"%f", Date3);

Nstimeinterval

The double type that represents the time interval in seconds

You can use the-initwithtimeintervalsincenow:? method to pass in a nstimeinterval parameter, create a NSDate object

For example: nsdate * tomorrowdate = [[NSDate alloc] initwithtimeintervalsincenow:24*60 * 60];

NSDate * yesterdaydate = [[NSDate alloc] initwithtimeintervalsincenow:-1 * 24*60 *60];

Interval of two time objects: Nstimeinterval = [Tomorrowdate timeintervalsincedate:yesterdaydate];

1NSDateFormatter *format =[[NSDateFormatter alloc] init];2[Format Setdateformat:@"YYYY-MM-DD HH:mm:ss"];3     //YYYY represents a 4-digit year4     //mm stands for two-bit months5     //DD stands for two-bit day6     //HH stands for 24 Hours of input7     //mm stands for two-bit minutes8     //SS stands for two-bit seconds9     //nsdateformatter function is nsstring and nsdateTen     //1.NSDate conversion to NSString OneNSString *datestr =[format stringfromdate:date]; ANSLog (@"%@", datestr); -      -NSDate *newdate = [format datefromstring:@"2014-07-02 11:54:03"]; theNSLog (@"%@", newdate);

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.