void getDate()
{
// 通过NSDate获取当前时间
NSDate *now = [NSDate date];
NSLog(@"Now date is: %@", now);
// 通过NSCalendar来过滤当前时间
NSCalendar *calendar = [NSCalendar currentCalendar];
NSUInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
NSDateComponents *dateComponent = [calendar components:unitFlags fromDate:now];
int year = [dateComponent year];
int month = [dateComponent month];
int day = [dateComponent day];
int hour = [dateComponent hour];
int minute = [dateComponent minute];
int second = [dateComponent second];
NSLog(@"year is: %d", year);
NSLog(@"month is: %d", month);
NSLog(@"day is: %d", day);
NSLog(@"hour is: %d", hour);
NSLog(@"minute is: %d", minute);
NSLog(@"second is: %d", second);
}
ios-通过NSCalendar来获取当前时间的年、月、日、时、分、秒
最新推荐文章于 2016-08-05 13:34:15 发布