ios时间时区处理
2015-09-24 17:18
489 查看
- (NSDate *)getNowDateFromatAnDate:(NSDate *)anyDate
{
//设置源日期时区
NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"UTC"];//或GMT
//设置转换后的目标日期时区
NSTimeZone* destinationTimeZone = [NSTimeZone localTimeZone];
//得到源日期与世界标准时间的偏移量
NSInteger sourceGMTOffset = [sourceTimeZone secondsFromGMTForDate:anyDate];
//目标日期与本地时区的偏移量
NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:anyDate];
//得到时间偏移量的差值
NSTimeInterval interval = destinationGMTOffset - sourceGMTOffset;
//转为现在时间
NSDate* destinationDateNow = [[[NSDate alloc] initWithTimeInterval:interval sinceDate:anyDate] autorelease];
return destinationDateNow;
}
NSDateFormatter *dateformatter = [[NSDateFormatter
alloc]init];
[dateformatter
setDateFormat:@"yyyy-MM-dd hh:mm:ss"];
NSString *dateStr = [dateformatter
stringFromDate:date];
{
//设置源日期时区
NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"UTC"];//或GMT
//设置转换后的目标日期时区
NSTimeZone* destinationTimeZone = [NSTimeZone localTimeZone];
//得到源日期与世界标准时间的偏移量
NSInteger sourceGMTOffset = [sourceTimeZone secondsFromGMTForDate:anyDate];
//目标日期与本地时区的偏移量
NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:anyDate];
//得到时间偏移量的差值
NSTimeInterval interval = destinationGMTOffset - sourceGMTOffset;
//转为现在时间
NSDate* destinationDateNow = [[[NSDate alloc] initWithTimeInterval:interval sinceDate:anyDate] autorelease];
return destinationDateNow;
}
NSDateFormatter *dateformatter = [[NSDateFormatter
alloc]init];
[dateformatter
setDateFormat:@"yyyy-MM-dd hh:mm:ss"];
NSString *dateStr = [dateformatter
stringFromDate:date];
相关文章推荐
- NSPredicate
- 读IOS进阶笔记。
- IOS中数据的存储方式总结 (待续。。。。。)
- iOS字体
- [IOS]本地通知
- 关于iOS中几种延时方式
- iOS 编程之 使用 Xcode6配置.pch文件
- iOS:KVO的概述与使用
- iOS 身份证号校验
- 关于 initWithNibName 和 loadNibNamed 的区别和联系
- iOS 9 分屏多任务
- IOS MD5 + Base64加密
- ios激情详解之通知
- iOS学习笔记(二)
- iOS扫描二维码的实现
- iOS图片的多选择,删除,和本地显示
- iOS多线程学习---线程同步
- 适配ios6,ios7等注意问题
- iOS 拍照获取照片 翻转90度的问题
- iOS求生之路一(tableview的分区)