3

I got current GMT time - 2013-05-15 08:55 AM

my current local time is - 2013-05-15 02:06 PM and time zone is - Asia/Kolkata

I tried to convert the above GMT to my local time with this code

   NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"Asia/Kolkata"];
   [dateFormatter setTimeZone:sourceTimeZone];


      // timeStamp - is the above GMT time

   NSDate *dateFromString = [dateFormatter dateFromString:timeStamp];
    NSLog(@"local time is %@",dateFromString);

but this give me wron time as - 2013-05-14 19:04:00 +0000 instead of 2013-05-14 02:06 PM

Why this happens? please help me to clear this.Thanks in advance

5 Answers5

7

Nothing is going wrong, a NSDate instance will never have a timezone ( well it will but it is always GMT, indicated by the +0000 timezone in the date description).

What you are telling dateformatter in your code is that the timezone of the date you want to parse is Asia/Kolkata but you want it to be GMT.

First you need to make a NSDate object from the input date string with the time zone set to the correct time zone, GMT as you indicated.

NSTimeZone *gmtTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
[dateFormatter setTimeZone:sourceTimeZone];
NSDate *dateFromString = [dateFormatter dateFromString:timeStamp];
NSLog(@"Input date as GMT: %@",dateFromString);

Then when you present the date as an string use :

NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithName:@"Asia/Kolkata"];
[dateFormatter setTimeZone:sourceTimeZone];
NSString *dateRepresentation = [dateFormatter stringFromDate:dateFromString];
NSLog(@"Date formated with local time zone: %@",dateRepresentation);
rckoenes
  • 69,092
  • 8
  • 134
  • 166
  • 1
    +1 but one minor nit pick. I'm fairly sure NSLog always logs NSDates in GMT format, so the text `local time is` is confusing. – JeremyP May 15 '13 at 11:11
5

The shortest way that I found to convert GMT time to your local timezone is

 NSDate* localDateTime = [NSDate dateWithTimeInterval:[[NSTimeZone systemTimeZone] secondsFromGMT] sinceDate:dateInGMT];

Furthermore, to convert this date value into string

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"HH:mm:ss"];//use the formatted as per your requirement. 

//Optionally for time zone converstions
[formatter setTimeZone:[NSTimeZone timeZoneWithName:@"..."]];

NSString *localTimeDateString = [formatter stringFromDate:localDateTime];
Ans
  • 3,461
  • 3
  • 23
  • 24
0
NSLog(@"local time is %@",[dateFormatter stringFromDate: dateFromString]);
Lithu T.V
  • 19,955
  • 12
  • 56
  • 101
  • but here I am getting - 2013-05-15 00:59 AM My local time is - 2013-05-15 02:29 PM –  May 15 '13 at 08:55
0
NSTimeZone* gmtTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
NSTimeZone* localTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"Asia/Kolkata"];

NSDateFormatter *gmtDateFormatter = [[NSDateFormatter alloc] init];
[gmtDateFormatter setTimeZone:gmtTimeZone];

NSDate *date = [gmtDateFormatter dateFromString:timestamp];

NSDateFormatter *localDateFormatter = [[NSDateFormatter alloc] init];
[localDateFormatter setTimeZone:localTimeZone];

NSLog(@"local time is %@",[localDateFormatter stringFromDate:date]);
Mike Pollard
  • 10,195
  • 2
  • 37
  • 46
0

Use timeZoneWithName

NSDate *date=[[NSDate alloc]init];
NSDateFormatter *timeFormat = [[NSDateFormatter alloc] init];
[timeFormat setTimeZone:[NSTimeZone timeZoneWithName:@"Asia/Kolkata"]];
[timeFormat setDateFormat:@"HH:mm"];
NSString *dateStr=[timeFormat stringFromDate:date];
user1548843
  • 670
  • 12
  • 20