//方法一
NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd"];
// [formatter setDateFormat:@"yyyy-MM-dd hh:mm:ss"];
NSString * locationString=[formatter stringFromDate:[NSDate date]];
NSLog(@"今天日期是:%@",locationString);
//方法二:
NSCalendar *calendar = [[NSCalendaralloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *now;
NSDateComponents *comps = [[NSDateComponentsalloc] init];
NSInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit |
NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
now=[NSDate date];
comps = [calendar components:unitFlags fromDate:now];
NSLog(@"comps=%@",comps);
int year=[comps year];
int week = [comps weekday];
int month = [comps month];
int day = [comps day];
int hour = [comps hour];
int min = [comps minute];
int sec = [comps second];
NSLog(@"year=%d,week=%d,month=%d,day=%d,hour=%d,min=%d,sec=%d",year,week,month,day,hour,min,sec);
NSLog(@"今天日期是:%d-%d-%d",year,month,day);