user3427791
user3427791

Reputation: 29

NSDate change time zone to GMT +02.00

I have a NSDate with time zone GMT +00.00 and I want to change it to GMT +02.00. To do this, I have created NSDateFormatter and set time zone to UTC but it does give me expected results,

Here is my code,

NSDateFormatter *timeformater = [[NSDateFormatter alloc] init];
[timeformater setLocale:[NSLocale currentLocale]];
[timeformater setDateFormat:@"EEEE-dd-MMMM-yyyy HH:mm a"];
[timeformater setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]];

How to change time zone correctly?

Upvotes: 0

Views: 407

Answers (1)

gnasher729
gnasher729

Reputation: 52622

NSDate is a point in time, independent of time zone. You can't change its time zone, because it doesn't have one. If we are in different areas, and call [NSDate date] at the same time (for example if I call you on the phone and we tap a button at the same moment), we get the same NSDate even though our watches show different times.

NSDateFormatter is used to create strings to display NSDate values, depending on time zone. NSCalendar is used for calculations with NSDate, depending on time zone. But you can't change the timezone of an NSDate.

Upvotes: 4

Related Questions