Combine Nscalendar and nsdate to do more date \ Time processing to get Nscalendar objects
Nscalendar *calendar = [Nscalendar Currentcalendar];? Acquired date
-(Nsdatecomponents *) components: (nscalendarunit) unitflags fromdate: (nsdate *) date;
Date Created
Nsdate?*d = [Nsdate?date];
Create Date Object
NSCALENDAR?*CA = [Nscalendar?currentcalendar];
Get Time component
nsdatecomponents?*cms= [ca?components:nscalendarunityear| nscalendarunitmonth| Nscalendarunitday Fromdate:d];
NSLog (@ "%ld-%ld-%ld", cms.year,cms.month,cms.day);
Compare the gap between two dates
-(Nsdatecomponents *) components: (nscalendarunit) unitflags fromdate: (nsdate *) startingdate toDate: (NSDate *) Resultdate options: (nscalendaroptions) opts;
NSString *time1 [email protected] "2014-04-08 20:50:40"; NSString *time2 [email protected] "2014-04-04 18:45:30";
NSDateFormatter *fmt = [[NSDateFormatter alloc] init]; Fmt.dateformat [email protected] "YYYY-MM-DD HH:mm:ss";
nsdate *date1 = [FMT datefromstring:time1]; NSDate *date2 = [FMT datefromstring:time2];
?//Take the time earlier??? [Date2?earlierdate:date1];? ? ? ?? Take the later one??? [Date2?laterdate:date1];
Convert a string to a date
? ? ? ? Similar to the top, the date format object is still required, but the method of invocation changes from Stringfromdate to
? ? ? ? This date was subtracted from the day because of the time zone setting problem
? ? ? ? Nsdateformatter?*format2=[[nsdateformatter?alloc]init];
? ? ? ? [format2?setdateformat:@ "Yyyy/mm/dd"];
? ? ? ? Nsstring?*[email protected] "2014/10/11";
? ? ? ? NSLog (@ "%@", [FORMAT2?DATEFROMSTRING:STR2]);
1. Create a Calendar object
Nscalendar?*calendar = [Nscalendar?currentcalendar];
2. Compare the time gap
Int?unit =? Nscalendarunityear?|? Nscalendarunitmonth?|? Nscalendarunitday
|? Nscalendarunithour?|? Nscalendarunitminute?|? Nscalendarunitsecond;?
Nsdatecomponents?*cmps = [calendar?components:unit?fromdate:date2?todate:date1?options:0];
NSLog (@ "%ld years%ld"%ld days%ld hours%ld minutes%ld seconds ", Cmps.year, Cmps.month, Cmps.day, Cmps.hour, Cmps.minute, cmps.second);
Calculate Date//1) The moment of Tomorrownstimeinterval secondsperday = 24*60*60;
NSDate *tomorrow = [NSDate datewithtimeintervalsincenow:secondsperday]; NSLog (@ "mydate =%@", tomorrow);
2) yesterday's present
Nstimeinterval secondsPerDay1 = 24*60*60;
nsdate *now = [NSDate date];nsdate *yesterday = [now addtimeinterval:-secondsperday1]; NSLog (@ "Yesterday =%@", yesterday);
Formatted date
Define NSDate
NSDATE?*D1 = [Nsdate?date];
Define a date time formatted class
Nsdateformatter?*formatter = [[Nsdateformatter?alloc]?init]; [Email protected] "YYYY-MM-DD HH:mm:ss";
Convert Date to Datastr
NSSTRING?*DATESTR = [FORMATTER?STRINGFROMDATE:D1];
iOS Intermediate-Date Time objects