oc-(Foundation framework-nsdate)

Source: Internet
Author: User
Tags dateformat

NSDate: is a class that handles DateTime in OC and can be used to represent time

Gets the current time

NSDate *d = [NSDate Date]; To create a date-time object

NSLog output is current time (east eight time zone)

Format the display of time

NSDate *d1 =[NSDate Date]; NSLog (@"%@", D1); //format date, time//NSDateFormatter Date Formatting    /*the YYYY represents a four-bit year mm representing a 2-bit month DD representing a 2-bit day HH representing a 24-hour HH 12-hour mm indicating a 2-bit number of minutes SS representing 2-bit seconds */NSDateFormatter*FOM = [NSDateFormatterNew]; //September 12, 2015Fom.dateformat =@"yyyy mm month DD Day HH:MM:SS"; NSString*datestr =[fom stringfromdate:d1]; NSLog (@"%@", DATESTR);

Calculation time

NSDateFormatter *fom = [NSDateFormatterNew]; //calculate the moment of tomorrow//24 hours after the time//Datewithtimeintervalsincenow is the time from nowFom.dateformat =@"yyyy mm month DD Day HH:MM:SS"; Nstimeinterval T= -* -* -; NSDate*tom =[NSDate datewithtimeintervalsincenow:t]; //format TimeNSString *timestr =[fom Stringfromdate:tom]; NSLog (@"%@", TIMESTR); //calculate yesterday's time-t//nsdate *tom = [NSDate datewithtimeintervalsincenow:-t];

Use of date-time objects

       // Date Object         // 2015-09-12 10:20:30        NSDate *d = [NSDate date];         // Create Date Object        Nscalendar *cal = [Nscalendar Currentcalendar];         // cal components: Get what parts of a date FromDate: Date Object        nsdatecomponents  *coms = [Cal components:nscalendarunityear| nscalendarunitmonth| Nscalendarunitday fromdate:d];        NSLog (@ "%ld%ld%ld", Coms.year, Coms.month, Coms.day);

oc-(Foundation framework-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.