Add +2 or -6 to the current GMT? (iphone)
i would like to create a tableView with different name of cities, and by clicking on each we can have the current time, i was thinking that i could put the GMT time and change it depending on the cell (the name of the city) we clicked : and thus, add +2 to the GMT hour, or -6 to the GMT. Do you think this could be possible?
I have this code for now, but i'm looking for some advices to create the "+2" or "-6" to this 开发者_运维问答code : (this code is located in a method, updated every second to have the hour-minute-second updated)
NSDate *myDate = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT"]];
[dateFormatter setDateFormat:@"dd-MM-YYYY"];
NSString *GMTDateString = [dateFormatter stringFromDate: myDate];
[dateFormatter release];
Thanks for your help
So, you want do display current time in different time zones? You can pass different time zones to dateFormatter and it'll convert date to that time zone and return it to you.
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT"]];
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:3600*2]]; // GMT+2
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:-3600*6]]; // GMT-6
Use the date formatter for this. You just set the timezone and then you can get correct dates.
//-6 hours
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:-21600]];
//+2 hours
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:7200]];
If you would like to account for Daylight Savings use a named time zone instead of -6.
精彩评论