2013-05-22 41 views
0

我正在从远程服务器检索某些日期。不幸的是可能的格式之一就是H:MM:数据的将时区:分钟:时区转换为另一个时区中的完整日期

实施例I昨天月,21,2013下载了:

1)下午4点36 EDT

2)下午11时51 EDT

数据服务器TimeZone是“America/New_York”。

我的资料TimeZone是“欧洲/罗马”。

这是我写的代码,直到今天早上早上5点我想它的工作:

NSDate *myDate = @"04:36PM EDT"; //11:51PM EDT (I know the date is May, 21 2013 but it is not in the data!) 

NSArray *component = [mydate componentsSeparatedByString:@" "]; 

//Remove any reference to the TimeZone 
//set it in the dateFormatter 

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
dateFormatter.dateFormat [email protected]"MM dd yyyy h:mma"; 
dateFormatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]; 
dateFormatter.timeZone = [[NSTimeZone alloc] initWithName:@"America/New_York"]; 

//Create a valid date for the hour 

NSDate *date = [NSDate date]; 
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; 
NSDateComponents *dateComponents = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) fromDate:date]; 

NSDate *dateObject = [self.dateFormatter dateFromString: [NSString stringWithFormat:@"%u %u %u %@", 
       dateComponents.month, 
       dateComponents.day, 
       dateComponents.year, 
       [component objectAtIndex:0]]]; 

//Convert to local timeZone    

dateFormatter.timeZone = [NSTimeZone localTimeZone]; // @"Europe/Rome" 
dateFormatter.dateStyle = NSDateFormatterShortStyle; 
dateFormatter.timeStyle = NSDateFormatterLongStyle; 


NSString *localTimeZoneDate = dateFormatter stringFromDate:[dateObject]; 

NSLog(@"Local Time Zone Date = %@", localTimeZoneDate); 

输出

虽然昨天的代码似乎正常工作,因为当我做了测试,我仍然在6小时的时间跨度,让我在同一天,5月21日,今天早上我发现它没有。问题在于创建正确的日期。

1)下午4点36 EDT ----> /05/2013 22点36分○○秒CEST

正确的输出应该已经/05/2013 22点36 CEST

2)下午11点51 EDT ----> /05/2013五点51分00秒CEST

正确的输出应该已经/05/2013 - 05:51 CEST

什么是处理这种特殊情况的最佳代码?

感谢

尼古拉

+0

在获得当天的日期组件之后,为什么不给它添加时间值?您应该尝试使用UTC工作,然后在末尾应用您的时区以进行显示。 – Wain

回答

0

现在我正在这样做:我看看当地时间的重新创建日期是否与当前当地时间相关。如果是,它应该足以从重新创建的日期中删除一天或多天。

0

尝试

NSString *dateString = @"04:36PM EDT"; //11:51PM EDT (I know the date is May, 21 2013 but it is not in the data!) 

NSArray *component = [dateString componentsSeparatedByString:@" "]; 

//Remove any reference to the TimeZone 
//set it in the dateFormatter 

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
dateFormatter.dateFormat [email protected]"MM dd yyyy"; 
dateFormatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]; 
dateFormatter.timeZone = [[NSTimeZone alloc] initWithName:@"America/New_York"]; 

NSString *tempDateString = [dateFormatter stringFromDate:[NSDate date]]; 
dateString = [tempDateString stringByAppendingFormat:@" %@",component[0]]; 

dateFormatter.dateFormat [email protected]"MM dd yyyy hh:mma"; 
NSDate *tempDate = [dateFormatter dateFromString:dateString]; 

dateFormatter.timeZone = [NSTimeZone localTimeZone]; 
dateFormatter.dateStyle = NSDateFormatterShortStyle; 
dateFormatter.timeStyle = NSDateFormatterLongStyle; 

NSString *localDateString = [dateFormatter stringFromDate:tempDate]; 

NSLog(@"Local Time Zone Date = %@", localDateString); 
+0

输出与我的代码相同:“04:36 PM EDT”转换为“22-05/2013 22:36:00 CEST”而不是“21 05 2013 22:36:00 CEST”。美国东部时间下午11:51的情况也是如此。 –

+0

@NicolaP。猜测工作很难做到。当你需要在两天后进行这种转换时,你打算如何做到这一点。最好在字符串中包含所有必要的信息。您可以使用格式化程序显示您想要显示的信息。 – Anupdas

+0

您提出了一个有效的观点,但不幸的是,我从服务器获得的日期字符串只是“04:36 PM EDT”,没有办法拥有另一种格式:(当超过一天的时间通过后,它会自动更改添加月份和那么我可以用高概率猜测,当我只看到小时:分钟:时区时,数据是关于今日(或根据时区的前一天)。如果是在我的时区5月22日,数据显示“04: 36PM EDT“我几乎可以肯定它指的是前一天在原来的时区 –

0

我没有检查这个,但我想这是因为你创建日历是上你的时区。在获取日期组件之前,尝试将日历时区设置为美国/纽约。

+0

最初我认为你的建议本来可以是一个简单而理想的解决方案,但后来我意识到它可以很容易地中断,在纽约和罗马之间有6个小时,原来的日期是“美国东部时间04:36 PM”(我们知道它指的是纽约的5月21日),这是一个“最后更新改变”,它不会再改变,直到第二天(5月22日纽约) 如果我ü在纽约的一天改变之前,你可以使用它。但假设我问欧洲/罗马的日期是5月22日15:00,纽约时间是22日5月9日上午。但是“美国东部时间04:36 PM”是指5月21日。 –

+0

@NicolaP。我没有关注。如果它在罗马是1500,在纽约是2100。 – JeremyP

+0

我解释一下。今天是5月25日。罗马和纽约(美国东部时间)之间有6个小时,所以如果是在罗马的15:00,纽约的时间是09:00。我检索的日期是指股票指数的收盘价,它将在纽约的16:30左右,22:30的罗马时间。因此,尽管是在罗马和纽约的5月25日,价格指的是到了5月24日,直到25日纽约市场重新开市之后才会发生变化。我确定将合成日期与当前当地日期进行比较。 –

相关问题