获取当前时间
NSDate *now = [NSDate date]; NSLog(@”now date is: %@”, now); 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);
计算年龄
第一种仅仅得出年份差的年龄
//计算年龄 NSString *birth = @"1993-10-30"; NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init]; [dateFormatter setDateFormat:@"yyyy-MM-dd"]; //生日 NSDate *birthDay = [dateFormatter dateFromString:birth]; //当前时间 NSString *currentDateStr = [dateFormatter stringFromDate:[NSDate date]]; NSDate *currentDate = [dateFormatter dateFromString:currentDateStr]; NSLog(@"currentDate %@ birthDay %@",currentDateStr,birth); NSTimeInterval time=[currentDate timeIntervalSinceDate:birthDay]; int age = ((int)time)/(3600*24*365); NSLog(@"year %d",age);
另外一种得出详细到天的年龄
NSCalendar *calendar = [NSCalendar currentCalendar];//定义一个NSCalendar对象 NSDate *nowDate = [NSDate date]; NSString *birth = @"1900-10-30"; NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init]; [dateFormatter setDateFormat:@"yyyy-MM-dd"]; //生日 NSDate *birthDay = [dateFormatter dateFromString:birth]; //用来得到详细的时差 unsigned int unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit; NSDateComponents *date = [calendar components:unitFlags fromDate:birthDay toDate:nowDate options:0]; if([date year] >0) { NSLog(@"%@",[NSString stringWithFormat:(@"%ld岁%ld月%ld天"),(long)[date year],(long)[date month],(long)[date day]]) ; } else if([date month] >0) { NSLog(@"%@",[NSString stringWithFormat:(@"%ld月%ld天"),(long)[date month],(long)[date day]]); } else if([date day]>0){ NSLog(@"%@",[NSString stringWithFormat:(@"%ld天"),(long)[date day]]); } else { NSLog(@"0天"); }
此项參考:http://blog.csdn.net/tt5267621/article/details/7720434
1、字符串转换为日期
NSDateFormatter* dateFormat = [[NSDateFormatter alloc] init];//实例化一个NSDateFormatter对象
[dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss"];//设定时间格式,这里能够设置成自己须要的格式
NSDate *date =[dateFormat dateFromString:@"1980-01-01 00:00:01"];
2、日期转换为字符串
NSDateFormatter* dateFormat = [[NSDateFormatter alloc] init];//实例化一个NSDateFormatter对象
[dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss"];//设定时间格式,这里能够设置成自己须要的格式
NSString *currentDateStr = [dateFormat stringFromDate:[NSDate date]];
3、字符串转int
Convert NSString to int
NSString *aNumberString = @"123";
int i = [aNumberString intValue];
4、int转字符串
Convert int to NSString
int aNumber = 123;
NSString *aString = [NSString stringWithFormat:@"%d", aNumber];