通过出生年月日计算年龄
来源:互联网 发布:手机视频慢动作软件 编辑:程序博客网 时间:2024/05/21 12:49
NSString *birth = @"1993-03-03";
// 通过NSDateFormatter将NSString 转换成 NSDate 格式
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd"];
NSDate *date = [dateFormatter dateFromString:birth];
// 通过NSDateComponents 从 date 中提取出 年月日
NSDateComponents *components1 = [[NSCalendar currentCalendar] components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond fromDate:date];
NSInteger year = [components1 year];
NSInteger month = [components1 month];
NSInteger day = [components1 day];
NSInteger hour = [components1 hour];
NSInteger minute = [components1 minute];
NSInteger second = [components1 second];
// 获取系统当前的年月日
NSDate *currentDate = [NSDate date]; // 获得系统的时间
NSDateComponents *components2 = [[NSCalendar currentCalendar] components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond fromDate:currentDate];
NSInteger currentYear = [components2 year];
NSInteger currentMonth = [components2 month];
NSInteger currentDay = [components2 day];
// 计算年龄
NSInteger iAge = currentYear - year - 1;
if ((currentMonth > month) || (currentMonth == month && currentDay >= day)) {
iAge++;
}
- 顶
- 0
- 通过出生年月日计算年龄
- 通过出生年月日计算年龄
- 通过身份证分析出生年月日、性别、年龄的SQL语句
- 根据出生年月日判断年龄
- 计算年月日年龄
- 根据身份证号计算:出生年月日和性别
- String类型出生年月计算年龄
- 根据数据库中的出生年份计算年龄
- java根据出生年月计算年龄
- php计算年龄精准到年月日
- 通过出生日期计算年龄
- 在EXCEL中根据身份证号码计算年龄、出生年月、性别
- sql server出生年月减去退休年月 计算年龄
- 通过出生年月得到当前年龄java代码实例
- 关于通过出生年月获取年龄的java代码
- Oracle 通过出生日期计算年龄
- android 通过datepicker计算年龄
- SQL 通过时间计算年龄
- swift_018(Swift 的结构体)
- mui如何增加自定义icon图标
- 图解HTTP读书笔记(九)
- Android EditText像TextView一样的样式
- JavaScript中局部变量与全局变量的不同
- 通过出生年月日计算年龄
- post请求参数的处理
- (第三季)601-socket编程-tcp服务器端;602-socket编程-tcp客户端
- swift_019(Swift 的类)
- php 第三方调用类。欢迎评论提意见
- mac 环境下 安装ga-cmd,命令行自动获取 google authenticator code
- Android获取屏幕宽高值
- Resin 启动多个服务
- HDU5956 The Elder(树上斜率DP)