NSDate: Acquisition and manipulation of time
1. Get the current time
1 // Get the current date
2 NSDate * date = send
2. Convert date to string and format
1 // date to string
2 NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
3 // YYYY: year MM: month dd: day HH: 24-hour clock hh: 12-hour clock
4 formatter.dateFormat = @ "YYYY-MM-dd HH: mm: ss";
5 NSString * strdate = [formatter stringFromDate: date];
6 NSLog (@ "% @", strdate);
3. Turn string back to date
1 // String to date
2 NSDate * date2 = [formatter dateFromString: strdate];
3 NSLog (@ "% @", date2);
4, the string date decomposition into the year, month, day
1 // Decompose date into year, month, and day
2 NSCalendar * calendar = [NSCalendar currentCalendar];
3 NSInteger unitflags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay;
4 NSDateComponents * components = [calendar components: unitflags fromDate: date];
5 NSLog (@ "year:% ld month:% ld day:% ld", [components year], [components month], [components day]);
5. Combine the year, month, and day into a date string
1 // Combining the year, month, and day into a date
2 [components setYear: 2014];
3 [components setMonth: 9];
4 [components setDay: 1];
5 NSDate * date3 = [calendar dateFromComponents: components];
6 NSLog (@ "% @", date3);
The main time forms of objective-c:nsdate