获取当前时间

        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. HBase性能测试

    hbase org.apache.hadoop.hbase.PerformanceEvaluationUsage: java org.apache.hadoop.hbase.PerformanceEv ...

  2. spring Annotation 笔记2.1

    使用注解替代xml 在前几章的笔记基础上添加使用注解的形式 1.配置applicationContext 添加context schema <?xml version="1.0&quo ...

  3. nginx自动切割访问日志

    Web 访问日志 (access_log) 记录了所有外部客户端对Web服务器的访问行为,包含了客户端IP,访问日期,访问的URL资源,服务器返回的HTTP状态码等重要信息. 一条典型的Web访问日志 ...

  4. 复习知识点:UITableView和UICollectionView的常用属性

    UITableView UICollectionView  //UICollectionViewLayout //UICollectionViewLayout决定了UICollectionView如何 ...

  5. (Java随机数举例)随机扔一千次硬币的正反次数

    方法一: public class coin{ public static void main(String args[]){ int n = 0; int m = 0; int len = 1000 ...

  6. FileUtil

    package com.wiseweb.util; import java.io.BufferedReader; import java.io.File; import java.io.FileInp ...

  7. ThinkPHP - 扩展个人类库 - 以验证码类为例子

    首先,在项目目录下创建Class文件夹,用于存储个人类文件. 之后建立Data目录存放所需字体文件,其他的数据也可以放在这个文件夹下. 然后再Conf文件夹下创建verify.php配置文件. 在co ...

  8. 随手记今天跟的几个iOS项目代码的问题

    休了一阵子假期,今天刚回来上班,项目代码已经有挺大的变化了,我就先体验.发现.跟进问题. 第一个问题是点击某个cell就挂掉的现象,同事表示必现但挺神奇.由于挂掉的时候没有啥有意义的信息,所以先简单粗 ...

  9. MySQL无法使用、导入中文数据乱码

    1,新版的MySQL无法使用 装的新版的mysql-installer-community-5.6.14.0.msi,无法使用(无法导入地图数据,卸载重装mysql_5.6.13.msi,无法启动). ...

  10. SDRAM 控制器的解析

    本篇博文非原创,是整理了网上的各家之言与一体,为自己以后方便查询所用.如有冒犯请告之. 1.Precharge与Refresh的区别? plj:两者都是对存储单元的电容进行充电.回写.但差异在于: P ...