iOS:发生时区问题

iOS:发生时区问题,ios,objective-c,iphone,timezone,nstimezone,Ios,Objective C,Iphone,Timezone,Nstimezone,看来苹果时区现在给我带来了麻烦。我测试了下面的代码,但它不能正常工作。这就是我目前所拥有的 NSDate *today = [NSDate date]; NSDateFormatter *estDf = [[NSDateFormatter alloc] init]; [estDf setTimeZone:[NSTimeZone timeZoneWithName:@"EST"]]; [estDf setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; NSSt

看来苹果时区现在给我带来了麻烦。我测试了下面的代码,但它不能正常工作。这就是我目前所拥有的

 NSDate *today = [NSDate date];

 NSDateFormatter *estDf = [[NSDateFormatter alloc] init];
 [estDf setTimeZone:[NSTimeZone timeZoneWithName:@"EST"]];
 [estDf setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

  NSString *stringDate = [estDf stringFromDate:today];
  NSDate *todaydateadjusted = [estDf dateFromString:stringDate];

  NSLog(@"Todays date is %@",[estDf stringFromDate:todaydateadjusted]);
这是我的NSLog响应。
今天的日期是2015-03-09 16:25:02


我以为是17:25:02,不是16:25:02。因为现在是东部时间下午5点,所以它应该打印17而不是16。还有其他人有这个问题吗?也许我的代码有问题

现在有夏令时。你不应该改用EDT吗

另一件事是时区名称与时区缩写。看看这个例子——我是用Swift编写的,但我认为它应该很容易理解并翻译成ObjC

var now = NSDate()
var df = NSDateFormatter()
df.timeZone = NSTimeZone(abbreviation: "EDT")
//df.timeZone = NSTimeZone(name: "EDT") <- wrong
df.dateFormat = "yyyy-MM-dd HH:mm:ss"
let edt = df.stringFromDate(now)
var now=NSDate()
var df=NSDateFormatter()
df.timeZone=NSTimeZone(缩写:“EDT”)

//df.timeZone=NSTimeZone(名称:“EDT”)您需要更改此选项:

[estDf setTimeZone:[NSTimeZone timeZoneWithName:@"EST"]];
为此:

[esfDf setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"EDT"]];

编辑:我试过EDT,结果也是一样。如果你碰巧和一个陌生人坐过德洛林的车,我羡慕你。正如答案所说,你的时间是错的:它正确地打印了18:30。请确保将EDT与缩写或America/New_York或类似名称一起用于时区名称。此时,您的代码是timeZoneWithName,但您传递了缩写。这给了我假时间。