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)