日期/时间转换为用户当地时间 - 问题
我的应用程序从远程服务器获取日期/时间,该服务器始终位于 GMT +1(UTC/GMT +1 小时)时区。
服务器提供的格式是:
2011 年 8 月 24 日 08:45
我想将此时间戳转换为用户时区的等效时间/日期(用户可以在世界任何地方)。
举个例子: 来自服务器的 24 08 2011 08:45PM 应在
24 08 2011 09:45PM 呈现给意大利用户(罗马)(GMT + 1)
此代码适用于某些时区,但我有一种不好的感觉,有一些非常错误的东西并且有一种更优雅的方式来做到这一点,
NSString *dateString = @"24 08 2011 09:45PM";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd MM yyyy hh:mma"];
NSDate *dateFromString = [[[NSDate alloc] init] autorelease];
dateFromString = [dateFormatter dateFromString:dateString];
NSDate* sourceDate = dateFromString;
NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"BST"];
NSTimeZone* destinationTimeZone = [NSTimeZone systemTimeZone];
NSInteger sourceGMTOffset = [sourceTimeZone secondsFromGMTForDate:sourceDate];
NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:sourceDate];
NSTimeInterval interval = destinationGMTOffset - sourceGMTOffset;
NSDate* destinationDate = [[NSDate alloc] initWithTimeInterval:interval sinceDate:sourceDate] ;
NSString *thePubDate = [dateFormatter stringFromDate:destinationDate];//[appLogic getPubDate];
NSLog(@"Result : %@",thePubDate);
[dateFormatter release];
//[dateFromString release];
[destinationDate release];
我将感谢您对此事的想法和建议
My application grabs a date / time from a remote server which is alway in GMT +1 (UTC/GMT +1 hour) timezone.
The format the server is providing is :
24 08 2011 08:45PM
I would like to convert this time stamp into the equivalent time/date of the users time zone (the user can be anywhere in the world).
thus as an example :
24 08 2011 08:45PM coming from the server should be presented
24 08 2011 09:45PM to an Italian user (rome) (GMT + 1)
This code works on some timezones but i have a bad feeling that there is something very wrong about it and that there is a much more elegant way to do it
NSString *dateString = @"24 08 2011 09:45PM";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd MM yyyy hh:mma"];
NSDate *dateFromString = [[[NSDate alloc] init] autorelease];
dateFromString = [dateFormatter dateFromString:dateString];
NSDate* sourceDate = dateFromString;
NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"BST"];
NSTimeZone* destinationTimeZone = [NSTimeZone systemTimeZone];
NSInteger sourceGMTOffset = [sourceTimeZone secondsFromGMTForDate:sourceDate];
NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:sourceDate];
NSTimeInterval interval = destinationGMTOffset - sourceGMTOffset;
NSDate* destinationDate = [[NSDate alloc] initWithTimeInterval:interval sinceDate:sourceDate] ;
NSString *thePubDate = [dateFormatter stringFromDate:destinationDate];//[appLogic getPubDate];
NSLog(@"Result : %@",thePubDate);
[dateFormatter release];
//[dateFromString release];
[destinationDate release];
I will appreciate your thoughts and suggestions on the matter
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只需在 dateFormatter 中设置时区,此代码就足够了
dateFromString 现在将具有日期 24 08 2011 08:45PM(GMT).. 然后将其转换为本地时间字符串只需编写以下代码,
Just set the timeZone in the dateFormatter, This code is enough
The dateFromString will now have the date 24 08 2011 08:45PM(GMT).. Then to convert this to string with local time just code the following,