您的位置:首页 > 移动开发 > IOS开发

IOS下获取时间、手机系统时区以及获取时间间隔

2015-07-10 09:16 323 查看
----------------------------------
1,获取系统时区

//获取系统时区

NSTimeZone *localTimeZone = [NSTimeZone
localTimeZone];

NSLog(@"localTimeZone is -->%@",localTimeZone);

NSTimeZone *systemTimeZone = [NSTimeZone
systemTimeZone];

NSLog(@"systemTimeZone -->%@",systemTimeZone);

NSTimeZone *defaultTimeZone = [NSTimeZone
defaultTimeZone];

NSLog(@"defaultTimeZone -->%@", defaultTimeZone);
2,获取当前时间
//获取系统当前时间
NSDate*currentDate
= [NSDatedate];
NSDateFormatter *dateformatter=[[NSDateFormatter alloc] init];
[dateformatter setDateFormat:@"YYYYMMdd"];
NSString *currentString=[dateformatter stringFromDate:currentDate];

NSLog(@"currentString:----------->%@",currentString);

3,获取一定间隔时间之后的日期

//一定间隔时间之后的日期
NSDate *date = [NSDatedate];
date = [datedateByAddingTimeInterval:-5*3600*24];

4,获取两个时间间隔,计算两个时间间隔

//创建日期格式化对象
NSDateFormatter *dateFormatter=[[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];

//创建了两个日期对象
NSDate *date1=[dateFormatter dateFromString:@"2010-3-3 11:00"];
NSDate *date2=[dateFormatter dateFromString:@"2010-3-4 12:00"];

//NSDate *date=[NSDate date];

//NSString *curdate=[dateFormatter stringFromDate:date];

//取两个日期对象的时间间隔:

//这里的NSTimeInterval并不是对象,是基本型,其实是double类型,是由c定义的:typedef
double NSTimeInterval;
NSTimeInterval time=[date2 timeIntervalSinceDate:date1];

int days=((int)time)/(3600*24);
int hours=((int)time)%(3600*24)/3600;
NSString *dateContent=[[NSString alloc] initWithFormat:@"%i天%i小时",days,hours];
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: