6

How to get the current time based on TimeZone in objective c?

example-> Current time for Asia/Kolkata.

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Neeraj Sonaro
  • 346
  • 1
  • 16
  • 5
    3 upvotes? Shocked... there are many sample over internet... – Fahim Parkar Oct 28 '17 at 09:09
  • 1
    Possible duplicate of [Convert UTC NSDate to local Timezone Objective-C](https://stackoverflow.com/questions/1268509/convert-utc-nsdate-to-local-timezone-objective-c) – clemens Oct 28 '17 at 09:20

1 Answers1

3

Try this..

    NSDateFormatter *dateFormatter = [NSDateFormatter new];
    [dateFormatter setDateFormat:@"dd/MM/yyyy HH:mm"];

    //Create the date assuming the given string is in GMT
    dateFormatter.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];

    //Create a date string in the local timezone
    dateFormatter.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:[NSTimeZone localTimeZone].secondsFromGMT];
    NSString *localDateString = [dateFormatter stringFromDate:[NSDate date]];
    NSLog(@"date = %@", localDateString);

and if you want specific timezone date then see below code..

NSDate *myDate = [NSDate date];
    NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
    [dateFormatter1 setDateStyle:NSDateFormatterLongStyle];
    [dateFormatter1 setTimeStyle:NSDateFormatterLongStyle];
    [dateFormatter1 setDateFormat:@"dd/MM/yyy hh:mm a"];

NSTimeZone *timeZone = [NSTimeZone timeZoneWithName: @"Asia/Calcutta"];
[dateFormatter1 setTimeZone:timeZone];

NSString *dateString = [dateFormatter1 stringFromDate: myDate];
NSLog(@"%@", dateString);
Nirav Kotecha
  • 2,493
  • 1
  • 12
  • 26