Reputation: 524
I am having trouble with timezones,
So here is the problem:
I have a date of GMT
2013-10-18 00:00:00 +0000
I convert it to GMT-10
[formatter setDateFormat:@"dd/MM/yyyy hh:mm a"];
[formatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT-10"]];
NSString *minusTenGMT = [formatter stringFromDate:date];
17/10/2013 02:00 PM
It gives me a string: 17/10/2013 02:00 PM
So basically minusTenGMT is a string with a string date
If I want to get the GMT value of minusTenGMT. I should just reverse the process
[formatter setDateFormat:@"dd/MM/yyyy hh:mm a"];
[formatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT"]];
NSDate *origDate = [formatter dateFromString:minusTenGMT];
But the thing is, the result of this is
2013-10-18 08:00:00 +0000 Compare to the orignal date of: 2013-10-18 00:00:00 +0000
, base on what I have searched the timezone of this string would be back to GMT? What is happening?.
But if I don't use the GMT abbreviation and I use my local timezone
[formatter setDateFormat:@"dd/MM/yyyy hh:mm a"];
[formatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:[[NSTimeZone localTimeZone] abbreviation]]];
NSDate *origDate = [formatter dateFromString:minusTenGMT];
The result is 2013-10-18 00:00:00 +0000 exactly the same as the orignal.
My local is GMT-10, I even tried changing my location through ios Simulator and system. They all go back to the original when the local is used (which is correct). But if the GMT they give out different outputs.
Can anybody tell me what is happening? I want to know why does it work if i use my own timezone instead of the original time zone of the date.
Thanks in Advance :D.
EDIT: Fixed the typo.
The thing that bothers me is why should I use GMT-10 when converting it back to GMT??
or I'm just too stupid to understand? I used GMT-10 format to get GMT-10.
so to get a GMT from GMT-10, I should use GMT format, but it is not working, I need to use GMT-10 format to get a GMT.
Upvotes: 0
Views: 705
Reputation: 15005
I agree with rmaddy Please try including this line below :--
formatter setDateFormat:@"dd/MM/yyyy hh:mm a"];
[formatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:[[NSTimeZone localTimeZone] abbreviation]]];
NSString *minusTenGMT = [formatter stringFromDate:date];// this line you need to include for converting
NSDate *origDate = [formatter dateFromString:minusTenGMT];
Upvotes: 1