一. //NSDate是一个日期的类
//1.date:无论在哪个时区,date获取的时间是相对应的零时区时间
NSDate *date=[NSDate date];
NSLog(@"%@",date);
//2.先获取当前所在的时区
NSTimeZone *zone=[NSTimeZone systemTimeZone];
NSLog(@"%@",zone);
//3.获取一下和0时区相差的秒数
NSInteger seconds=[zone secondsFromGMTForDate:date];
NSLog(@"%ld",seconds);
//4.通过相差的秒数,能获取到现在的时间
NSDate *localDate=[NSDate dateWithTimeIntervalSinceNow:seconds];
NSLog(@"%@",localDate);
NSDate *times=[NSDate dateWithTimeIntervalSince1970:0];
NSLog(@"%@",times); 1970-01-01 00:00:00 +0000
//5.找到明天的这个时间
NSDate *tomorrowTime=[NSDate dateWithTimeIntervalSinceNow:(seconds+24*3600)];
NSLog(@"%@",tomorrowTime);
//6.找到昨天的这个时间
NSDate *yesterdayTime=[NSDate dateWithTimeIntervalSinceNow:(seconds-24*3600)];
NSLog(@"%@",yesterdayTime);
//7.时间间隔,对应的double类型
//计算两个时间对象的时间间隔
NSTimeInterval interval=[tomorrowTime timeIntervalSinceDate:date];
NSLog(@"%g",interval);
//8.计算当前时间和固定时间的差值
NSDate *constTime=[NSDate dateWithTimeIntervalSinceNow:(seconds-7 *3600)];
NSTimeInterval intervalTime=[constTime timeIntervalSinceDate:date];
if (intervalTime < 60) {
NSLog(@"刚刚");
}else if (intervalTime >=60 && intervalTime<=3600){
NSLog(@"%ld分钟前",(NSInteger)intervalTime/60);
}else if (intervalTime > 3600 && intervalTime<3600*24){
NSLog(@"%ld小时前",(NSInteger)intervalTime/3600);
}
打印结果:1小时前