获取当前时间

        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天");
}

字符串、日期、Int转换

此项參考: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 获取当前时间以及计算年龄(时间差)的更多相关文章

  1. Swift3.0 iOS获取当前时间 - 年月日时分秒星期

    Swift3.0 iOS获取当前时间 - 年月日时分秒星期func getTimes() -> [Int] { var timers: [Int] = [] // 返回的数组 let calen ...

  2. iOS 获取当前时间格式化字符串

    iOS 获取当前时间格式化字符串 太阳火神的漂亮人生 (http://blog.csdn.net/opengl_es) 本文遵循"署名-非商业用途-保持一致"创作公用协议 转载请保 ...

  3. golang 时间戳 时间格式化 获取当前时间 timestamp 计算时间差

    获取当前时间 func Now func Now() Time 1 Now returns the current local time. func (Time) UTC func (t Time) ...

  4. JS获取服务器时间并且计算距离当前指定时间差的函数

    项目中遇到了从服务器获取时间,现在记录一下方便以后查询: 1.后台代码:(创建一个date对象并以JSON的形式返回去) // 获取服务器时间 public String getNowServerTi ...

  5. iOS 时间戳和时间互换,计算两日期相隔天数

    /*  *获取当前系统时间的时间戳  */ +(NSInteger)getNowTimestamp;   /**  * 获取当前时间  */ + (NSString *)getNowTimeTampF ...

  6. iOS - 获取当前时间日期星期几

    //获取当前时间日期星期 - (NSString *)getCurrentTimeAndWeekDay { NSArray * arrWeek=[NSArray arrayWithObjects:@& ...

  7. iOS - 获取系统时间年月日,阳历(公历)日期转农历的方法

    //获取当前时间 NSDate *now = [NSDate date]; NSLog(@" now date is: %@ ",now); NSCalendar *calenda ...

  8. iOS获取网络时间与转换格式

      [NSDate date]可以获取系统时间,但是会造成一个问题,用户可以自己修改手机系统时间,所以有时候需要用网络时间而不用系统时间.获取网络标准时间的方法: 1.先在需要的地方实现下面的代码,创 ...

  9. iOS获取本地时间

    NSDate *currentDate = [NSDate date];//获取当前时间,日期 NSDateFormatter *dateFormatter = [[NSDateFormatter a ...

随机推荐

  1. [移动网关]2G环境下资源下载有一定概率失败,客户端日志显示收到403错误

    2G环境下资源下载有一定概率失败,客户端日志显示收到403错误 问题现象: 测试同学在使用联通号码在移动网络环境下,访问连接得到的response_code出现是403,导致资源读取失败表情显示异常. ...

  2. Maven--几个需要补充的问题(三)

    <Maven--搭建开发环境(一)> <Maven--构建企业级仓库(二)> <Maven—几个需要补充的问题(三)> 前两篇由于篇幅太长,为了给读者理解方便,这篇 ...

  3. linux搭建java环境

    建议使用EXCEL查看 准备文件 apache-tomcat-7.0.57.tar tomcat web容器 server-jre-7u76-linux-x64.tar  java jdk java执 ...

  4. Unity Notes调制粒子系统的颗粒的最大数目

    Unity该粒子系统是很容易使用.这样的问题是在实际的过程中遇到的:以控制的粒子系统组件的动态需要可产生颗粒的最大数目. 看doc他说,有maxParticles控制.却没有这个开放的參数.仅仅能通过 ...

  5. 【转】理解RESTful架构

    [转]理解RESTful架构 越来越多的人开始意识到,网站即软件,而且是一种新型的软件. 这种"互联网软件"采用客户端/服务器模式,建立在分布式体系上,通过互联网通信,具有高延时( ...

  6. iOS转场动画

    文顶顶 最怕你一生碌碌无为 还安慰自己平凡可贵 iOS开发UI篇—核心动画(转场动画和组动画) iOS开发UI篇—核心动画(转场动画和组动画) 一.转场动画简单介绍 CAAnimation的子类,用于 ...

  7. BZOJ 1270: [BeijingWc2008]雷涛的小猫( dp )

    简单的dp.. dp(i,j) = max(dp(x,y))+cnt[i][j], (x,y)->(i,j)是合法路径. 设f(i)= max(dp(x,y))(1≤x≤N, 1≤y≤i), g ...

  8. instanceof 变量是否属于某一类 class 的实例

    <?phpclass MyClass{} class NotMyClass{}$a = new MyClass;$b = new NotMyClass;var_dump($a instanceo ...

  9. maven 构建web项目index.jsp报错

    The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path... 解决 ...

  10. POJ 2451 Uyuw's Concert(半平面交nlgn)

    //#pragma comment(linker, "/STACK:16777216") //for c++ Compiler #include <stdio.h> # ...