1. Get the year, month and day
NSDateFormatter *formater = [[ NSDateFormatter alloc] init];
[formater setDateFormat:@"yyyy-MM-dd"];// 时间格式
NSDate *currentDate = [NSDate date];//获取当前日期
NSString *currentDateStr = [formater stringFromDate:currentDate];
2. Get year, month, day, hour, minute, and second
Just change the time format [formater setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDateFormatter *formater = [[ NSDateFormatter alloc] init];
[formater setDateFormat:@"yyyy-MM-dd HH:mm:ss"];// 时间格式
NSDate *currentDate = [NSDate date];//获取当前日期
NSString *currentDateStr = [formater stringFromDate:currentDate];