NSDate Change Time Zone

I am writing an application that includes a time zone. I want to capture the time of the user device, called dateA , and I have the target date, which will always be in the time zone of EDT ( dateB ).

I want to get the difference between two dates and show the user, but in dateB timezone.

eg:
user device time is 07:30 AM PDT, and dateB is 11:00 Eastern time.
Thus, the time difference will be 30 minutes.

My algorithm:
1) Get user device time
2) convert to EDT
3) capture the time difference between dateA and dateB

My problem is that after getting user device time [NSDate date] and go DateFormatter with EDT DateFormatter . Time does not change.

EDIT ::

  NSDate *localDate = [NSDate date]; //this will have 7:30AM PDT NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; dateFormatter.dateFormat = @"yyyy-MM-dd HH:mm:ss"; [dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"EDT"]]; NSString *convertedTimeString = [dateFormatter stringFromDate:localDate]; 

How come convertTimeString does not contain 10:30 in EDT? What am I doing wrong?

+4
source share
1 answer

A NSDate kept in neutral with time zone. Prior to NSDateFormatter for actual formatting for a given time zone using - [NSDateFormatter setTimeZone:] . If you want a string from NSDate , there is also [NSDate descriptionWithCalendarFormat: timeZone: locale:] , but it's usually better to use NSDateFormatter .

+2
source

Source: https://habr.com/ru/post/1495933/


All Articles