2013-04-03 175 views
0

我在转换设备当前(本地)日期和时间的日期和时间时遇到问题。 我从API获取格式2013-04-03 01:07:56 +0000,我想以本地日期和时间格式转换此日期并显示。任何人都可以请建议我如何实现这一目标?将日期和时间转换为当地日期和时间格式

回答

2

只需设置在dateFormatter时区,此代码是足够

NSString *dateString = @"24 08 2011 09:45PM"; 
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
[dateFormatter setDateFormat:@"dd MM yyyy hh:mma"]; 
NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"BST"]; 
[dateFormatter setTimeZone:sourceTimeZone]; 
NSDate *dateFromString = [dateFormatter dateFromString:dateString]; 

的dateFromString现在将有日期24 08 2011下午8点45分(格林尼治标准时间)。然后将其转换为字符串与本地时间只是代码如下,

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
[formatter setTimeZone:[NSTimeZone localTimeZone]]; 
[formatter setLocale:[NSLocale systemLocale]]; 

[dateFormatter setDateFormat:@"dd MM yyyy hh:mma"]; 
NSString *stringFromDAte = [dateFormatter stringFromDate:dateString]; 
+0

谢谢Kirti,但我想根据设备格式显示日期格式。在这里我的应用也支持本地化。 – Vijay

+0

@Vijay关注此URl http://stackoverflow.com/questions/1081647/how-to-convert-time-to-the-timezone-of-the-iphone-device –

+0

感谢您的建议+1 – Vijay

相关问题