计算两个时间的差 年龄计算
来源:互联网 发布:mac apowersoft 编辑:程序博客网 时间:2024/05/01 08:17
/** * 计算两个时间的差 * * @param birthDay 开始计算的日子(生日) * @param nowDate 计算截止的日子(今天/可以传nil) */+ (NSDateComponents *)componentsDateFrom:(NSDate *)birthDay toDate:(NSDate *)nowDate{ if (!nowDate) { nowDate = [NSDate date]; } //用来得到详细的时差 unsigned int unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond; //定义一个NSCalendar对象 NSCalendar *calendar = [NSCalendar currentCalendar]; 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:(@"0-%ld-%ld"),(long)[date month],(long)[date day]]); } else if([date day]>0){ NSLog(@"%@",[NSString stringWithFormat:(@"0-0-%ld"),(long)[date day]]); } else { NSLog(@"%@",@"0-0-0"); } return date;}
得到 (NSDateComponents *)date 对象之后 就可以直接 [date year]、[date month]、[date day] 来获取相差多少天 也就是我if判断中的代码了
1 0
- 计算两个时间的差 年龄计算
- 计算两个时间的差
- 计算两个时间的差
- C#中计算两个时间的差
- 计算两个时间之差的函数
- C#中计算两个时间的差
- C#中计算两个时间的差
- C#中计算两个时间的差
- C#中计算两个时间的差
- C#中计算两个时间的差
- C#中计算两个时间的差
- 计算两个时间的日期差
- php 计算两个时间的差
- 计算两个时间戳的差
- php计算两个时间戳的差
- php计算两个时间之间的差
- 计算两个时间之差
- 输入身份证号码计算两者的年龄差
- 19. 试用vSphere 6(六):VCSA(6.0.0.2175370)的安装与配置
- linux vim 快捷键
- hdu 5676ztr loves lucky numbers
- leetcode 240 Search a 2D Matrix II java 算法
- 决策树—学习心得
- 计算两个时间的差 年龄计算
- P12证书和描述文件
- WebGIS实现在线要素编辑之ArcGIS Server 发布Feature Service 过程解析
- jquery和thinkphp利用formData属性ajax上传并,添加水印,裁剪,实现预览
- 20. 利用Veeam ONE监控vSphere虚拟化平台(Veeam ONE安装及配置)
- Arbitrage(bellman,最长路径)
- Unity集成内嵌网页插件-支持Android和IOS
- 【旧代码整理】uri路由处理的重要组成部分 Model_Route
- WebGIS实现在线要素编辑之ArcGIS Server 发布Feature Service 过程解析