0

I've written a method that accepts a NSDate object and should turn it into a NSDate object with EST time zone. The way I've seen other people accomplish this is by using a date formatter to change the date to a string with the specified time zone. The issue is when I try to change that string back to a NSDate object using "dateFromString".

- (NSDate*)turnToEST:(NSDate*)date
{
NSLog(@"turnToEST called with date %@", date);

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

NSString *stringFromDate = [dateFormatter stringFromDate:date];

NSLog(@"date formatted is %@", stringFromDate);

NSDate *dateFromString = [[NSDate alloc] init];

NSDateFormatter *dateFormatter2 = [[NSDateFormatter alloc]init];
[dateFormatter2 setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

dateFromString = [dateFormatter2 dateFromString:stringFromDate];

NSLog(@"date has been changed to %@", dateFromString);

return date;
}

With output...

turnToEST called with date 2013-12-19 14:15:17 +0000
date formatted is 2013-12-19 09:15:17
date has been changed to 2013-12-19 14:15:17 +0000

I'm not sure why this is any different than

Converting NSString to NSDate (and back again)

Community
  • 1
  • 1
SCar88
  • 110
  • 1
  • 4
  • 13
  • `- (NSDate*)turnToEST:(NSDate*)date { return date; }` :P NSDate objects are not subject to time zones, they are absolute. – albertamg Dec 19 '13 at 14:42

1 Answers1

4

NSDate values do not have an associated time zone, they represent an abstract moment in time. So "a NSDate object with EST time zone" isn't a thing that exists. Time zones only come into play when formatting them for output, or trying to do calendar-based math.

NSLog always uses UTC when printing its output.

So you're taking a moment in time, formatting it to a string in a particular time zone, and then parsing it back into the same moment in time. That's working as intended.

Sixten Otto
  • 14,816
  • 3
  • 48
  • 60
  • There are a few incorrect statements in this answer. 1) NSDate is not abstract. It is the number of seconds since a specific epoc in a specific time zone. 2) Logging an NSDate always shows the date in UTC time, not local time. – rmaddy Dec 19 '13 at 15:02
  • So the value of an NSDate can not be changed? Only the appearance of it when you output? – SCar88 Dec 19 '13 at 15:21
  • And an NSDate value will always be in GMT. That's just the way it is and nothing can be done about it. So whenever dateFromString is called how does it know that the string is being shown in EST? So, for example a string consists of 06:00:00 and then dateFromString is called on it. How does it know that this 06:00:00 isn't already in GMT? – SCar88 Dec 19 '13 at 15:54
  • Is this because the computer clock is in EST so it knows to move up 5 hours? – SCar88 Dec 19 '13 at 20:29
  • @rmaddy you're confusing how the NSDate value is stored internally with what it represents. Yes, the epoch does need to be a value in a specific time zone. But what that epoch is, and in which time zone it is, don't really matter in terms of reasoning about NSDates or string conversion. – Sixten Otto Dec 19 '13 at 20:40
  • @rmaddy I've corrected the error re: NSLog; thanks for calling that out. – Sixten Otto Dec 19 '13 at 20:49