I have the following code:
self.formatter = [[NSDateFormatter alloc] init];
[formatter setTimeZone:timeZone];
[formatter setDateFormat:@"YYYY-MM-dd hh:mm:ss a z"];
timeStampForCity = [formatter stringFromDate:[NSDate date]];
NSLog(@"%@", timeStampForCity);
updateDate = [formatter dateFromString:timeStampForCity];
NSLog(@"%@", updateDate);
NSLog(@"%@", timeStampForCity);
outputs:
2011-08-01 10:05:50 PM EDT
but NSLog(@"%@", updateDate);
outputs:
2011-08-02 02:05:50 +0000
I need updateDate
to display the time in the c开发者_C百科urrent Timezone (e.g., as seen in the first NSLog() output). How do I do this properly?
EDIT 1
I also have this code:
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit| NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit | NSTimeZoneCalendarUnit;
NSDateComponents *dateComponents = [gregorian components:unitFlags fromDate:[NSDate date]];
[dateComponents setTimeZone:[NSTimeZone timeZoneWithName:cityNameFormatted]];
updateDate = [gregorian dateFromComponents:dateComponents];
[gregorian release];
but the Timezones it displays are off for many of the regions.
Thanks ahead of time for your help.
Here we go again...
A date is an independent point in time. It exists and will always exist, regardless of whether or not humans and our time measurement devices exist. Time zones are an invention of man to help us not go crazy. The jury's still out on whether or not they're effective at that. :P
When you talk about wanting a "date in EDT or any other timezone", you've failed to grasp what a date is. A timezone is simply one aspect of how to display a point in time.
So, you have an NSDate
. This date is the correct date, regardless of what timezone you're in. So how do you "display the time in the current time zone"? You use an NSDateFormatter
.
An NSDate
is inherently unreadable. There are ways that we can represent a date, such as an interval of seconds (which is an arbitrarily long interval itself) from a different point in time. However, to make a date human readable can only be accomplished by formatting it into a string. So, let's do that:
NSDate *myDate = [NSDate date]; // or whatever date you want to make human-readable
NSDateFormatter *f = [[NSDateFormatter alloc] init];
[f setDateStyle:NSDateFormatterLongStyle];
[f setTimeStyle:NSDateFormatterLongStyle];
NSArray *timeZoneNames = [NSTimeZone knownTimeZoneNames];
for (NSString *name in timeZoneNames) {
NSTimeZone *tz = [NSTimeZone timeZoneWithName:name];
[f setTimeZone:tz];
NSLog(@"%@", [f stringFromDate:date]);
}
Now, obviously the output for this is going to be enormously long (mine is over 400 lines long). But here's a sampling of that output:
...
August 2, 2011 1:03:38 AM GMT-03:00
August 2, 2011 12:03:38 AM AST
August 2, 2011 12:03:38 AM GMT-04:00
August 1, 2011 11:03:38 PM EST
August 2, 2011 1:03:38 AM GMT-03:00
August 1, 2011 11:03:38 PM CDT
August 2, 2011 12:03:38 AM AST
August 2, 2011 1:03:38 AM GMT-03:00
August 1, 2011 10:03:38 PM CST
August 2, 2011 12:03:38 AM AST
August 2, 2011 12:03:38 AM GMT-04:00
August 1, 2011 11:03:38 PM GMT-05:00
August 1, 2011 10:03:38 PM MDT
August 1, 2011 10:03:38 PM MDT
August 2, 2011 12:03:38 AM GMT-04:00
August 1, 2011 11:03:38 PM CDT
August 1, 2011 11:33:38 PM GMT-04:30
August 2, 2011 1:03:38 AM GMT-03:00
August 1, 2011 11:03:38 PM EST
August 1, 2011 11:03:38 PM CDT
August 1, 2011 10:03:38 PM MDT
...
So, what do we see here? The same date has a whole bunch of different string representations! NEAT! In other words, the timezone of the formatter is what's used in determining what the outputted string looks like.
And so: the date itself has NOTHING to do with the timezone. If you want a date to look a certain way, you have to configure the formatter to make it look that way.
Update From your comment, I think I get what you want. You want the date components formatted for a certain timezone? In other words, you have a point in time, and you want to know what hour/minute/second that is in (say) the US Eastern timezone?
If that's the case, then you do that based on the calendar:
NSDate *date = [NSDate date];
NSCalendar *cal = [NSCalendar currentCalendar];
[cal setTimeZone:[NSTimeZone timeZoneWithName:@"EST"]];
NSDateComponents *components = [cal components:NSUIntegerMax fromDate:date];
NSLog(@"%@", components);
And this will give you:
<NSDateComponents: 0x100124240>
Calendar: <CFCalendar 0x100133e90 [0x7fff7605cea0]>{identifier = 'gregorian'} / Time Zone: EST (GMT-05:00) offset -18000
Era: 1
Year: 2011
Month: 8
Day: 2
Hour: 9
Minute: 47
Second: 21
Week of Month: 1
Week of Year: 31
Year for Week of Year: 2011
Weekday: 3
Weekday Ordinal: 1
Quarter: 0
精彩评论