[iPhone/iPad] iPhone/iPad 时区转换
2011-05-27 14:57
232 查看
iPhone/iPad 时区转换
罗朝辉(http://blog.csdn.net/kesalin)
转载请注明出处
在服务器使用的时区与用户本地时区不一致的情况下,如果需要显示从服务器得来的时间,我们需要进行必要的时区和显示格式转换。
其转换过程为:
获取源(服务器)NSDateFormatter,用这 NSDateFormatter 得 dateFromString 方法获得源时区的时间。
然后计算两个时区的时间偏差量,用这个偏差量加上前面计算得到的源时区时间就得到用户本地时区的时间了。
参见代码:
参数 srcFormat:是源时区时间的显示格式
参数 dstFormat:是本地时区时间的显示格式
- (NSString *)localeTimeZoneTimeWithSrcFormat:(NSString *)srcFormat dstFormat: (NSString *) dstFormat
{
NSDateFormatter *usFormatter = [[[NSDateFormatter alloc] init] autorelease];
usFormatter.locale = [[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"] autorelease];
[usFormatter setDateFormat:srcFormat];
NSDate *srcDate = [usFormatter dateFromString:self];
NSTimeZone *srcTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"EST"]; // America/New_York
NSTimeZone *dstTimeZone = [NSTimeZone localTimeZone];
NSInteger srcGMTOffset = [srcTimeZone secondsFromGMTForDate:srcDate];
NSInteger dstGMTOffset = [dstTimeZone secondsFromGMTForDate:srcDate];
NSTimeInterval interval = dstGMTOffset - srcGMTOffset;
NSDate *dstDate = [[[NSDate alloc] initWithTimeInterval:interval sinceDate:srcDate] autorelease];
NSDateFormatter *localeFormatter = [NSString localeDateFormatterInstance];
[localeFormatter setDateFormat:dstFormat];
NSString *formattedDateString = [localeFormatter stringFromDate:dstDate];
return formattedDateString;
}
罗朝辉(http://blog.csdn.net/kesalin)
转载请注明出处
在服务器使用的时区与用户本地时区不一致的情况下,如果需要显示从服务器得来的时间,我们需要进行必要的时区和显示格式转换。
其转换过程为:
获取源(服务器)NSDateFormatter,用这 NSDateFormatter 得 dateFromString 方法获得源时区的时间。
然后计算两个时区的时间偏差量,用这个偏差量加上前面计算得到的源时区时间就得到用户本地时区的时间了。
参见代码:
参数 srcFormat:是源时区时间的显示格式
参数 dstFormat:是本地时区时间的显示格式
- (NSString *)localeTimeZoneTimeWithSrcFormat:(NSString *)srcFormat dstFormat: (NSString *) dstFormat
{
NSDateFormatter *usFormatter = [[[NSDateFormatter alloc] init] autorelease];
usFormatter.locale = [[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"] autorelease];
[usFormatter setDateFormat:srcFormat];
NSDate *srcDate = [usFormatter dateFromString:self];
NSTimeZone *srcTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"EST"]; // America/New_York
NSTimeZone *dstTimeZone = [NSTimeZone localTimeZone];
NSInteger srcGMTOffset = [srcTimeZone secondsFromGMTForDate:srcDate];
NSInteger dstGMTOffset = [dstTimeZone secondsFromGMTForDate:srcDate];
NSTimeInterval interval = dstGMTOffset - srcGMTOffset;
NSDate *dstDate = [[[NSDate alloc] initWithTimeInterval:interval sinceDate:srcDate] autorelease];
NSDateFormatter *localeFormatter = [NSString localeDateFormatterInstance];
[localeFormatter setDateFormat:dstFormat];
NSString *formattedDateString = [localeFormatter stringFromDate:dstDate];
return formattedDateString;
}
相关文章推荐
- [Cocoa] iPhone/iPad 时区转换
- iPhone下xib文件批量转换成iPad下xib文件
- iphone ipad viewController不响应横竖屏转换相关消息的问题
- [iOS]iPad和iPhone版本xib文件相互转换
- 在Xcode4 中将iPhone使用的xib转换成iPad使用的xib
- Xcode IPhone xib 转换到 IPad
- iPhone下xib文件批量转换成iPad下xib文件
- iPhone的xib与iPad的xib相互转换
- (iPhone/iPad开发)NSData与NSArray互相转换
- 使用mencoder转换flv为ipad/iphone下能播放的mp4格式
- iphone程序转换为ipad程序
- iPhone中将特定时区的某个时间与当前所在时区或其他时区时间相互转换
- ios storyboard中 iphone 和iPad 之间的转换方式
- ios 开发中 中关于ipad和iphone工程的转换以及nib文件的转换
- 如何将xib文件再ipad和iphone之间互相转换
- iphone/ipad/itouch进入DFU模式最简单的操作办法
- 基于jQuery的判断iPad、iPhone、Android是横屏还是竖屏的代码
- How are the icon files in my application bundle used on iPad and iPhone
- ipad和iPhone在使用UIAlertController的时候出现的错误
- 各代iphone ipad iPod各种信息 获取设备型号等等整理