Question

My current Device time is --- 2013-05-09 10:23 AM

I convert it int GMT with following code

   NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
   dateFormatter.dateFormat = @"yyyy-MM-dd HH:mm a";

   NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
  [dateFormatter setTimeZone:gmt];
   NSString *timeStamp = [dateFormatter stringFromDate:[NSDate date]];
   NSLog(@"time in GMT ------ %@",timeStamp);

And I got the out put as

     time in GMT ------  2013-05-10 04:53 AM

then I found my time zone

  NSTimeZone *localTime = [NSTimeZone systemTimeZone];
  NSLog(@"local timezone  is------ %@",[localTime name]);

out put

  local timezone  is------ Asia/Kolkata

Now I need to convert the above GMT time to local time.I used the following code

   NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:[localTime name]];
  [dateFormatter setTimeZone:sourceTimeZone];
   NSDate *dateFromString = [dateFormatter dateFromString:timeStamp];
   NSLog(@"local time is ------- %@",dateFromString);

But I am getting a wrong out put..

   local time is ------- 2013-05-09 19:23:00 +0000

Why this happen ? I should get local time as - 2013-05-09 10:23 AM

Was it helpful?

Solution 2

// 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);

OTHER TIPS

Your timestamp string is in GMT. This means that when you want to convert the GMT string back to an NSDate, your date formatter needs to be set to GMT, not your local timezone. By doing this you will get the proper NSDate.

And don't forget that when you log an NSDate object, it will display in GMT regardless of anything else.

Please my dear do little effort on google means first search your question on google and if you don't get your answer in that case put your question on stackover flow. And see your answer on below link....

  1. iPhone: NSDate convert GMT to local time
  2. iphone time conversion gmt to local time
  3. Date/time conversion to user's local time - issue
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.dateFormat = @"yyyy-MM-dd HH:mm a";

    NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
    [dateFormatter setTimeZone:gmt];
    NSString *timeStamp = [dateFormatter stringFromDate:[NSDate date]];
    NSLog(@"time in GMT ------ %@",timeStamp);



    NSTimeZone *localTime = [NSTimeZone systemTimeZone];
    NSLog(@"local timezone  is------ %@",[localTime name]);


    NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
    [dateFormatter setTimeZone:sourceTimeZone];
    NSDate *dateFromString = [dateFormatter dateFromString:timeStamp];
    NSLog(@"local time is ------- %@",dateFromString);


    NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
    dateFormatter1.dateFormat = @"yyyy-MM-dd HH:mm";

    NSTimeZone *gmt1 = [NSTimeZone localTimeZone];
    [dateFormatter1 setTimeZone:gmt1];
    NSString *timeStamp1 = [dateFormatter1 stringFromDate:[NSDate date]];
    NSLog(@"local time is ------- %@",timeStamp1);

Output:

time in GMT ------ 2013-05-10 05:13 AM
 local timezone  is------ Asia/Kolkata
 local time is ------- 2013-05-10 00:13:00 +0000
 local time is ------- 2013-05-10 10:43

Try Using This Code :

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm a"];

    // get time zone
    [dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];
    NSDate *date = [NSDate date];
    NSString *timeStamp = [dateFormatter stringFromDate:[NSDate date]];
    date = [dateFormatter dateFromString:timeStamp];

    // change time zone
    [dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"Asia/Kolkata"]];
    // or you can use SystemTimeZone
    // [dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];

    NSString *strDateWithLocalTimeZone = [dateFormatter stringFromDate:date];

    NSLog(@"Local Time Zone Date %@", strDateWithLocalTimeZone);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top