获取当前时间

        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. Windows Phone 8初学者开发—第3部分:编写第一个Windows Phone 8应用程序

    原文 Windows Phone 8初学者开发—第3部分:编写第一个Windows Phone 8应用程序 原文地址: http://channel9.msdn.com/Series/Windows- ...

  2. 删除 Windows Azure 网站上的标准服务器头

    编辑人员注释: 本文章由 Windows Azure 网站团队的项目经理 Erez Benari 撰写. 请求和响应中包含的 HTTP 头是Web 服务器和浏览器之间的 HTTP 通信过程的一部分.例 ...

  3. Android Testing(1) 浅尝Android测试的奥秘

    ------- 源自梦想.永远是你IT事业的好友.只是勇敢地说出我学到! ---------- 仅供学习和交流使用,翻译不好勿喷,请只摘除不合适的地方 Testing The Android fram ...

  4. 随机生成A~Z的字母CharDemo

  5. layout_weight属性详解

    看到上面这段代码,大家肯定认为三个TextView的宽度是1:2:2,但实际上是如图1这样的,宽度之比确实是1:2:2,但为什么第一个和后两个是齐平的呢?下面我给大家画一条线,可以看到虽然控件是没有对 ...

  6. 怎样让jQuery和其它js库共存

    怎样让jQuery和其它js库共存 有时候需要同时使用jQuery和其它javascript,比如在joomla中默认的是motools,但很多人还是希 望能够使用jQuery,如果直接调用的话,由于 ...

  7. Android中EditText,Button等控件的设置

    EditText可以使用:TextView.setEnabled(true)来设置为可编辑,其实很简单,写在这里以便以后自己查看. Button设置可用性:setVisibility(View.VIS ...

  8. vs2010如何打开vs2013

    vs2010如何打开vs2013 百度经验有,简单实用.

  9. 在线QQ客服

    代码一: <a target="_blank" href="http://wpa.qq.com/msgrd?v=3&uin=451314789&si ...

  10. Krita 3.0 发布,KOffice 的图像处理器(刺激一下自己的神经)

    Krita 3.0 发布了,经历了一年多的开发,动画功能被集成到Krita核心,改善了绘画功能,可及时预览绘画结果,该版本也是最新移植到QT的版本. 查看完整发布说明,可以点击这里. 下载地址: Wi ...