0

In my iPhone / iPad app I am showing a UIDatePicker for time. It will display time in this format, 11:00 AM. When User clicks on the time row we expand the time row to display this datePicker row. I have a time stamp in string "starts": "11:00", // time of day in ISO-8601 format I need to show this on the picker wheel as selected time when it gets opened up. For this, first of all I get the date at 12 AM using https://stackoverflow.com/a/9040554/4082792. Then I convert the given time (11:00) to number of seconds and add it to the midnight time to get the current time. This time is in local timezone (as I specify the timezone while using NSDateFormatter). When I try to setDate to UIDatePicker with this date, It gives me incorrect time, even though the time is correct in the NSDate variable. For 11:00 AM, it gives me 6:40 while the local time is 4:30. So, I have two questions : 1) Why is the time wrong on wheel. 2) How can I convert the NSDate from one timezone to another, I need to show it in the local time format.

Snippet :

NSString *strDate = @"11:00";
        NSDate *date = [NSDate date];
        NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
        NSUInteger preservedComponents = (NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit);
        date = [calendar dateFromComponents:[calendar components:preservedComponents fromDate:date]];


        ///Start time
        NSString *startTime = @"11:00";
        NSArray *startTimeSeparatedByColon = [startTime componentsSeparatedByString:@":"]; /// From 22:10 to [22, 10];
        NSInteger hourPartOfStart = startTimeSeparatedByColon[0] ? [startTimeSeparatedByColon[0] integerValue] : 0;
        NSInteger minutePartOfStart = startTimeSeparatedByColon[1] ? [startTimeSeparatedByColon[1] integerValue] : 0;
        NSTimeInterval totalTime = (hourPartOfStart*60*60+minutePartOfStart*60);

        NSDate *finalDate =   [NSDate dateWithTimeInterval:totalTime sinceDate:date];

        NSDate *dt = [NSDate dateWithTimeInterval:[NSTimeZone localTimeZone].secondsFromGMT sinceDate:finalDate];
        self.datePicker.date = dt;
Community
  • 1
  • 1
iosCurator
  • 4,356
  • 2
  • 21
  • 25

2 Answers2

0

By default, iOS converts date into the device's time zone. But if you want to convert date into another time zone, here is the code for that:

NSTimeZone *currentDateTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"EST"];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:currentDateTimeZone];

You can get the date in "EST" time zone from this dateFormatter object.

Aneeq Anwar
  • 1,282
  • 7
  • 20
0

Convert "EDT" TimeZone

 NSString *format = @"EEEE dd-MMMM-yyyy HH:mm:ss z";
 NSDateFormatter *date_EDTDateFormate = [[NSDateFormatter alloc] init];
 date_EDTDateFormate setDateFormat:format];
 date_EDTDateFormate setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"EDT"]];
 NSString *stringEDT = [dateFormatter_EDT stringFromDate:date_System];
Kirit Modi
  • 23,155
  • 15
  • 89
  • 112