0

I have searched a lot Regarding this issue on Google but Did,t find Proper Solution. All Result Gives Timezone in Date and time format.

I want to Know that How to get Only Timezone of Device in GMT Format. Like, I want only this

+5:30

as TimeZone. How can i achive this ?

I have tried this

 NSTimeZone* currentTimeZone = [NSTimeZone localTimeZone];

but it will give result in kolkatta/Delhi . I have also tried other answer but didn't find solution.

Badal Shah
  • 7,541
  • 2
  • 30
  • 65

3 Answers3

1

My suggestion is to look at Apple Documentation for NSTimeZone.

If you do, you became aware that there is a method -secondsFromGMT that returns seconds form the Greenwich time.
Maybe you can also create your own date formatter string to pass as -dateFormat property of an NSDateFormatter and get directly a string representation of time zone. Something like that @"ZZZZZ".
Click here for info on how to code correctly a date format.

Duncan C
  • 128,072
  • 22
  • 173
  • 272
Andrea
  • 26,120
  • 10
  • 85
  • 131
  • Creating a date formatter that only returns the offset from GMT sounds like the correct solution. (voted.) That way you don't have to worry about fussy formatting issues. The date formatter would do it for you. – Duncan C Jan 20 '16 at 13:53
1

Try this:

    NSDate *myDate = [NSDate date];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateStyle:NSDateFormatterLongStyle];
    [dateFormatter setTimeStyle:NSDateFormatterLongStyle];
    [dateFormatter setDateFormat:@"ZZZ"];

     NSTimeZone *timeZone = [NSTimeZone timeZoneWithName: @"Asia/Kolkata"];
    [dateFormatter setTimeZone:timeZone];

    NSString *dateString = [dateFormatter stringFromDate: myDate];
    NSLog(@"%@", dateString);

OUTPUT:

+0530

For more information read the @Andrea answer

Ramesh_T
  • 1,251
  • 8
  • 19
0
-(void)getCurrentTimeZoneMins
{
    NSTimeZone* destinationTimeZone = [NSTimeZone systemTimeZone];
    float timeZoneOffset = [destinationTimeZone secondsFromGMTForDate:[NSDate date]] / 60;;
    NSLog(@"%f mins",timeZoneOffset);
}

-(void)printTimeZone
{
    NSDateFormatter *localTimeZoneFormatter = [NSDateFormatter new];
    localTimeZoneFormatter.timeZone = [NSTimeZone localTimeZone];
    localTimeZoneFormatter.dateFormat = @"Z";
    NSString *localTimeZoneOffset = [localTimeZoneFormatter stringFromDate:[NSDate date]];
    NSLog(@"%@",localTimeZoneOffset);
}
Amit Tandel
  • 883
  • 7
  • 16