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];
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];
相关文章推荐
- IOS图片压缩上传服务器终极解决方案
- ld: -pie can only be used when targeting iOS 4.2 or later clang: error: linker command failed with e
- iOS开发 替换NSLog打印设置
- ld: -pie can only be used when targeting iOS 4.2 or later clang: error: linker command failed with e
- iOS开发之控件ContentMode代码改变填充模式小结
- IOS多线程基本使用
- IOS design patterns
- ios 判断是qq,银行卡,手机号等等公用的方法
- iOS多工程协同开发
- iOS中MRC和ARC混编
- iOS基础(c)—指针
- 汉语字典或者词典的简单的ios小demo
- iOS 多线程技术由浅深入(学习笔记)
- 苹果开发 笔记(46)圆角
- iOS 开发怎么入门?2
- iOS 开发中 SIGPIPE信号的分析跟处理
- iOS 的本地化使用和创建过程
- 小白学开发(ios)基础C_函数(2015-07-09)
- iOS - 地图 - 百度地图环境配置
- IOS 多线程-GCD学习总结