iOS 获取当前时间以及计算年龄(时间差)
获取当前时间
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];
iOS 获取当前时间以及计算年龄(时间差)的更多相关文章
- Swift3.0 iOS获取当前时间 - 年月日时分秒星期
Swift3.0 iOS获取当前时间 - 年月日时分秒星期func getTimes() -> [Int] { var timers: [Int] = [] // 返回的数组 let calen ...
- iOS 获取当前时间格式化字符串
iOS 获取当前时间格式化字符串 太阳火神的漂亮人生 (http://blog.csdn.net/opengl_es) 本文遵循"署名-非商业用途-保持一致"创作公用协议 转载请保 ...
- golang 时间戳 时间格式化 获取当前时间 timestamp 计算时间差
获取当前时间 func Now func Now() Time 1 Now returns the current local time. func (Time) UTC func (t Time) ...
- JS获取服务器时间并且计算距离当前指定时间差的函数
项目中遇到了从服务器获取时间,现在记录一下方便以后查询: 1.后台代码:(创建一个date对象并以JSON的形式返回去) // 获取服务器时间 public String getNowServerTi ...
- iOS 时间戳和时间互换,计算两日期相隔天数
/* *获取当前系统时间的时间戳 */ +(NSInteger)getNowTimestamp; /** * 获取当前时间 */ + (NSString *)getNowTimeTampF ...
- iOS - 获取当前时间日期星期几
//获取当前时间日期星期 - (NSString *)getCurrentTimeAndWeekDay { NSArray * arrWeek=[NSArray arrayWithObjects:@& ...
- iOS - 获取系统时间年月日,阳历(公历)日期转农历的方法
//获取当前时间 NSDate *now = [NSDate date]; NSLog(@" now date is: %@ ",now); NSCalendar *calenda ...
- iOS获取网络时间与转换格式
[NSDate date]可以获取系统时间,但是会造成一个问题,用户可以自己修改手机系统时间,所以有时候需要用网络时间而不用系统时间.获取网络标准时间的方法: 1.先在需要的地方实现下面的代码,创 ...
- iOS获取本地时间
NSDate *currentDate = [NSDate date];//获取当前时间,日期 NSDateFormatter *dateFormatter = [[NSDateFormatter a ...
随机推荐
- commons-logging和slf4j都是日志的接口
过上面的图,可以简单的理清关系! commons-logging和slf4j都是日志的接口,供用户使用,而没有提供实现! log4j,logback等等才是日志的真正实现. 当我们调用接口时,接口的工 ...
- iOS多线程中的单例
#import "MyHandle.h" static MyHandle *handle = nil; @implementation MyHandle // 传统写法 // 此时 ...
- 【组队赛三】-C cf448B
Suffix Structures Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Submit S ...
- emacs打开乱码解决办法
前言:有时候我们使用emacs打开文件的时候,因为emacs默认编码跟文档编码不同而出现了乱码如图: 对于新手的我们应该通过以下两种途径来解决: 方法一: 只需C-x <RET> r ( ...
- MySQL无法使用、导入中文数据乱码
1,新版的MySQL无法使用 装的新版的mysql-installer-community-5.6.14.0.msi,无法使用(无法导入地图数据,卸载重装mysql_5.6.13.msi,无法启动). ...
- ITextSharp 初次接触
官网:http://www.itextpdf.com/ (英文好的建议看这里) 下面我就对itextsharp做一个初步的介绍,并把最近封装的一个用于生成pdf的类库提供给需要的朋友,对于大神你可以 ...
- tomcat编译通过问题
tomcat 编译后 的类 和 网站目录不能同名!
- JS给元素增加className
function(element,value) //给元素添加className { if(!element.className) { element.className=value; } else{ ...
- Android常用动画Frame-By-Frame Animations的使用
在Android的动画中有一种叫做Frame by Frame 的动画效果,就是跟Flash播放一样,是一帧一帧地显示,如果动画是连续并且有规律的话,就跟播放视频一样. 首先在drawable目录下添 ...
- VC++在对话框中加入属性页
当一个基于对话框的程序中有相当多的控件时,你一定会想到使用属性页来将这些控件分类放置.本文针对这种方法来讨论几种可能实现的方案. 方案一本方案的例子请见源代码打包文件中的Property1部分 在对话 ...