NSDateFormat获取
NSDate *date =[NSDate date];NSDateFormatter *formatter = [[NSDateFormatter alloc]init];[formatter setDateFormat:@"yyyy"];NSInteger currentYear=[[formatter stringFromDate:date] integerValue];[formatter setDateFormat:@"MM"];NSInteger currentMonth=[[formatter stringFromDate:date]integerValue];[formatter setDateFormat:@"dd"];NSInteger currentDay=[[formatter stringFromDate:date] integerValue];NSLog(@"currentDate = %@ ,year = %ld ,month=%ld, day=%ld",date,currentYear,currentMonth,currentDay);
NSDateComponents获取
NSDate *currentDate = [NSDate date];NSCalendar *calendar = [NSCalendar currentCalendar];NSDateComponents *components = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:currentDate];NSInteger year=[components year];NSInteger month=[components month];NSInteger day=[components day];NSLog(@"currentDate = %@ ,year = %ld ,month=%ld, day=%ld",currentDate,year,month,day);
ps: 获取年月日的时候不要加上8个小时,iOS系统会自动帮我们自动加上八个小时