3

I need to set local timezones as per the location of the ios device.

[dateFormat setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]]; 

or

[dateFormat setTimeZone:[NSTimeZone timeZoneWithName:@"GTC+5.30"]]; 

is suited?

David Rönnqvist
  • 56,267
  • 18
  • 167
  • 205
Jan49
  • 59
  • 1
  • 2
  • 8
  • Do you try with this? http://stackoverflow.com/questions/11504843/get-current-iphone-device-timezone-date-and-time-from-utc-5-timezone-date-and-ti OR http://stackoverflow.com/questions/19186666/get-timezone-country-from-iphone – nmh May 28 '14 at 08:53
  • 2
    `[NSTimeZone localTimeZone]` ? – Ossir May 28 '14 at 08:55

4 Answers4

11
// get current date/time
NSDate *today = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
// display in 12HR/24HR (i.e. 11:25PM or 23:25) format according to User Settings
[dateFormatter setTimeStyle:NSDateFormatterShortStyle];
NSString *currentTime = [dateFormatter stringFromDate:today];
[dateFormatter release];
NSLog(@"User's current time in their preference format:%@",currentTime);

You can get the name of the local time zone using:

NSTimeZone *timeZone = [NSTimeZone localTimeZone];
NSString *tzName = [timeZone name];
Rohit
  • 2,646
  • 6
  • 27
  • 52
0

This is how I do it:

    NSString *strDateFormat = @"yyyy-MM-dd'T'HH:mm:ss.SSS";

    // ---------------------------------------------------------
    // input date is UTC date time, need to convert that time
    // to user's own local device timezone to get correct date
    // ---------------------------------------------------------

    NSDateFormatter *dateFormatterUTC = [[NSDateFormatter alloc] init];
    [dateFormatterUTC setDateFormat:strDateFormat];
    [dateFormatterUTC setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];

    // get UTC Date first
    NSDate* utcDate = [dateFormatterUTC dateFromString:strDate];

    // get local date from UTC Date
    NSDateFormatter *dateFormatterLocal = [[NSDateFormatter alloc] init];
    [dateFormatterLocal setDateFormat:strDateFormat];
    [dateFormatterLocal setTimeZone:[NSTimeZone systemTimeZone]];

    // get final local date calculated by offsetting UTC Date
    NSDate *localDate = [dateFormatterUTC dateFromString:[dateFormatterLocal stringFromDate:utcDate]];

    NSDate *localNowDate = [dateFormatterUTC dateFromString:[dateFormatterLocal stringFromDate:[NSDate date]]];
Zhang
  • 11,549
  • 7
  • 57
  • 87
0
NSString *dateStr = @"2012-07-16 07:33:01";
NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init]; 
[dateFormatter1 setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; 
NSDate *date = [dateFormatter1 dateFromString:dateStr];
NSLog(@"date : %@",date);

NSTimeZone *currentTimeZone = [NSTimeZone localTimeZone];
NSTimeZone *utcTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"UTC"];

NSInteger currentGMTOffset = [currentTimeZone secondsFromGMTForDate:date];
NSInteger gmtOffset = [utcTimeZone secondsFromGMTForDate:date];
NSTimeInterval gmtInterval = currentGMTOffset - gmtOffset;

NSDate *destinationDate = [[[NSDate alloc] initWithTimeInterval:gmtInterval sinceDate:date] autorelease];

NSDateFormatter *dateFormatters = [[NSDateFormatter alloc] init];
[dateFormatters setDateFormat:@"dd-MMM-yyyy hh:mm"];
[dateFormatters setDateStyle:NSDateFormatterShortStyle];
[dateFormatters setTimeStyle:NSDateFormatterShortStyle];
[dateFormatters setDoesRelativeDateFormatting:YES];
[dateFormatters setTimeZone:[NSTimeZone systemTimeZone]];  
dateStr = [dateFormatters stringFromDate: destinationDate];
NSLog(@"DateString : %@", dateStr);

- (Courtesy: Yuvaraj.M)
Nitesh Borad
  • 4,583
  • 36
  • 51
0

You can use the localTimeZone class method of NSTimeZone class to get a relative time zone object that decodes itself to become the default time zone for any locale in which it finds itself.

SRC: https://developer.apple.com/library/mac/documentation/cocoa/reference/foundation/classes/NSTimeZone_Class/Reference/Reference.html#//apple_ref/occ/clm/NSTimeZone/localTimeZone

If you need latitude and longitude: Get the official time zone database at http://www.iana.org/time-zones. Download tzdata2013g.tar.gz. There's a file in that archive named zone.tab that gives a lat/long for each time zone, for example:

 CH      +4723+00832     Europe/Zurich

The +4723+00832 indicates latitude = +47º23", longitude = +8º23" (SRC:Get Timezone/country from iPhone)

Community
  • 1
  • 1
Bikram Thapa
  • 1,329
  • 1
  • 16
  • 29