日期/时间转换为用户当地时间 - 问题

发布于 2024-11-30 22:44:44 字数 1450 浏览 2 评论 0原文

我的应用程序从远程服务器获取日期/时间,该服务器始终位于 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

臻嫒无言 2024-12-07 22:44:44

只需在 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 08:45PM(GMT).. 然后将其转换为本地时间字符串只需编写以下代码,

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd MM yyyy hh:mma"];
NSString *stringFromDAte = [dateFormatter stringFromDate:dateString];

Just set the timeZone in the dateFormatter, This code is enough

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];

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,

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd MM yyyy hh:mma"];
NSString *stringFromDAte = [dateFormatter stringFromDate:dateString];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文